require 'fileutils'
require 'qiniu'
require 'digest'
require 'openssl'
require 'base64'
require 'time'
require 'json_builder'


class ProjectApi < Grape::API
	#通用接口
	version 'v1', using: :path
	format :json
	default_format :json
	formatter :json, Grape::Formatter::Rabl
	resource :project do
		###########管理员可以查看，普通用户可以查看#####
		desc '查询项目接口'
		params do
			optional :id, type: String, desc: '项目ID'
			optional :user_id, type: String, desc: '人员ID'
		end
		get 'detail' do
			content_type 'application/x-www-form-urlencoded'
			if params[:id].nil?
				{code: '400', msg: '缺少项目ID'}
			else
				project = Project.where(id: params[:id]).first
				if project.nil?
					{code: '400', msg: '项目不存在'}
				else
					project_user = ProjectUser.where(project_id: params[:id], user_id: params[:user_id]).first
					if project_user.nil? || project.project_user_url.nil? || project.project_user_url.empty?
						project_user_url = ProjectApi::make_project_user(params[:id])
						if project_user_url.empty?
							project_user_url = ""
						else
							 project.project_user_url = project_user_url
							 project.save
						end
					else
						project_user_url =  project.project_user_url
					end
					project.score = ProjectApi.project_score(project.id)
					if project.certification_company.nil?
						company_name = ""
						certification_status = 0
					else
						company = Company.find(project.certification_company)
						company_name = company.company_name
						certification_status = project.certification_status
					end
					if project_user.nil?
						{code: '0', msg: 'OK',
								project:{
										company_name: company_name,
										certification_status: certification_status,
										name: project.name,
										score: project.score,
										lng: project.lng,
										lat: project.lat,
										area_radius: project.area_radius.nil? ? -1000 : project.area_radius,
										project_user_url: project_user_url,
										join_time: ""
								}
						}
					else
						{code: '0', msg: 'OK',
							project:{
									company_name: company_name,
									certification_status: certification_status,
									name: project.name,
									score: project.score,
									lng: project.lng,
									lat: project.lat,
									area_radius: project.area_radius.nil? ? -1000 : project.area_radius,
									project_user_url: project_user_url,
									join_time: project_user.join_time.nil? ? "" : project_user.join_time.strftime("%Y-%m-%d")
							}
						}
					end
				end
			end
		end
		########管理员可见########
		desc '查询项目简介'
		params do
			optional :project_id, type: String, desc: '项目ID'
		end
		get 'introduction' do
			if params[:project_id].nil?
				{code: '400', msg: '缺少项目ID'}
			else
				project = Project.where(id: params[:project_id], publish_status: 1).first
				if project.nil?
					{code: '400', msg: '查询不到项目或项目已关闭'}
				else
					project_desc = project.project_desc.nil? ? "" : project.project_desc
					{code: '0', msg: 'OK', project_desc: project_desc}
				end
			end
		end

		desc '更新默认项目'
		params do
			optional :project_id, type: String, desc: '项目ID'
			optional :user_id, type: String, desc: '用户ID'
		end
		post 'change_project' do
			if params[:project_id].nil?
				{code: '400', msg: '缺少项目ID'}
			elsif params[:user_id].nil?
				{code: '400', msg: '缺少默认项目ID'}
			else
				project = Project.where(id: params[:project_id], publish_status: 1).first
				if project.nil?
					{code: '400', msg: '查询不到项目或项目已关闭'}
				else
					user_detail = UserDetail.where(user_id: params[:user_id]).first
					if user_detail.nil?
						{code: '400', msg: '人员ID错误'}
					else
						user_detail.project_id = params[:project_id]
						if user_detail.save
							{code: '0', msg: 'OK'}
						else
							{code: '400', msg: '更新失败'}
						end
					end
				end
			end
		end

		desc '查询加入的项目列表'
		params do
			optional :user_id, type: String, desc: '用户ID'
			optional :page, type: Integer, desc: '页码'
		end
		post 'my_project' do
			per_page = 10
			if params[:user_id].nil?
				{code: '400', msg: '缺少用户ID'}
			else
				if UserAPI.auth_user(params[:user_id]) == -99
					result = Hash.new
					result["code"] = "0"
					result["msg"] = "OK"
					if params[:page].nil? || params[:page] == 1
						projects = Project.all.order(create_at: :desc).limit per_page
						project_count = Project.all.count
						project_list = []
						projects.each do |p|
							project_info = Hash.new
							project_info["id"] = p.id
							project_info["name"] = p.name
							project_info["join_time"] = ""
							project_info["exit_time"] = ""
							project_list << project_info
						end
						result["projects"] = project_list
						result["count"] = project_count
						if params[:page].to_i == 1
							result["previous_page"] = ""
						elsif  params[:page].nil?
							result["previous_page"] = ""
						else
							result["previous_page"] = (params[:page].to_i - 1).to_s
						end
						if project_count > per_page
							result["next_page"] = "2"
						else
							result["next_page"] = ""
						end
						result
					else
					  project_count = Project.all.count
						offset = (params[:page].to_i - 1) * per_page
						if project_count > offset
							projects = Project.all.order(create_at: :desc).limit(per_page).offset(offset)
							project_list = []
							projects.each do |p|
								project_info = Hash.new
								project_info["id"] = p.id
								project_info["name"] = p.name
								project_info["join_time"] = ""
								project_info["exit_time"] = ""
								project_list << project_info
							end
							result["projects"] = project_list
							result["count"] = project_count
							result["previous_page"] = ""
							if project_count > (params[:page].to_i * per_page)
								result["next_page"] = (params[:page].to_i + 1).to_s
							else
								result["next_page"] = ""
							end
						else
							project_list = []
							result["projects"] = project_list
							result["count"] = project_count
							if params[:page].to_i == 1
								result["previous_page"] = ""
							elsif  params[:page].nil?
								result["previous_page"] = ""
							else
								result["previous_page"] = (params[:page].to_i - 1).to_s
							end
							result["next_page"] = ""
						end
						result
					end
				else
					result = Hash.new
					result["code"] = "0"
					result["msg"] = "OK"
					if params[:page].nil? || params[:page] == 1
						project_users = ProjectUser.where("user_id = ? and join_time IS NOT NULL", params[:user_id]).order(join_time: :desc).limit per_page
						project_count = ProjectUser.where("user_id = ? and join_time IS NOT NULL", params[:user_id]).count
						project_list = []
						project_users.each do |pu|
							project_info = Hash.new
							project = Project.where(id: pu.project_id).first
							project_info["id"] = project.id
							project_info["name"] = project.name
							project_info["join_time"] = (pu.join_time.nil? ? "" : pu.join_time.strftime("%Y-%m-%d"))
							project_info["exit_time"] = (pu.exit_time.nil? ? "" : pu.exit_time.strftime("%Y-%m-%d"))
							project_list << project_info
						end
						result["projects"] = project_list
						result["count"] = project_count
						if params[:page].to_i == 1
							result["previous_page"] = ""
						elsif  params[:page].nil?
							result["previous_page"] = ""
						else
							result["previous_page"] = (params[:page].to_i - 1).to_s
						end
						if project_count > per_page
							result["next_page"] = "2"
						else
							result["next_page"] = ""
						end
						result
					else
					  project_count = ProjectUser.where("user_id = ? and join_time IS NOT NULL", params[:user_id]).count
						offset = (params[:page].to_i - 1) * per_page
						if project_count > offset
							project_users = ProjectUser.where("user_id = ? and join_time IS NOT NULL", params[:user_id]).order(join_time: :desc).limit(per_page).offset(offset)
							project_list = []
							project_users.each do |pu|
								project_info = Hash.new
								project = Project.where(id: pu.project_id).first
								project_info["id"] = project.id
								project_info["name"] = project.name
								project_info["join_time"] = (pu.join_time.nil? ? "" : pu.join_time.strftime("%Y-%m-%d"))
								project_info["exit_time"] = (pu.exit_time.nil? ? "" : pu.exit_time.strftime("%Y-%m-%d"))
								project_list << project_info
							end
							result["projects"] = project_list
							result["count"] = project_count
							result["previous_page"] = ""
							if project_count > (params[:page].to_i * per_page)
								result["next_page"] = (params[:page].to_i + 1).to_s
							else
								result["next_page"] = ""
							end
						else
							project_list = []
							result["projects"] = project_list
							result["count"] = project_count
							if params[:page].to_i == 1
								result["previous_page"] = ""
							elsif  params[:page].nil?
								result["previous_page"] = ""
							else
								result["previous_page"] = (params[:page].to_i - 1).to_s
							end
							result["next_page"] = ""
						end
						result
					end
				end
				
			end
		end
  		
		desc '查询项目'
		params do
			optional :project_name, type: String, desc: '项目名称'
			optional :user_id, type: String, desc: '用户ID'
			optional :page, type: Integer, desc: '页码'
		end
		post '/search_project' do
			per_page = 10
			result = Hash.new
			result["code"] = "0"
			result["msg"] = "OK"
			if params[:project_name].nil?
				{code: "400", msg: "请输入项目名称"}
			elsif params[:user_id].nil?
				{code: "400", msg: "请输入用户ID"}
			else
				if (/^[0-9]*$/ =~ params[:project_name]).nil?
					if params[:page].nil? || params[:page] == 1
						project_name = params[:project_name]
						projects = Project.where("name LIKE ? and publish_status = 1", "%#{project_name}%").order(create_at: :desc).limit per_page
						project_count = Project.where("name LIKE ?  and publish_status = 1", "%#{project_name}%").count
						project_list = []
						projects.each do |p|
							project = Hash.new
							project_user = ProjectUser.where(project_id: p.id, user_id: params[:user_id]).first
							project["id"] = p.id
							project["name"] = p.name
							project["score"] = p.score
							if project_user.nil?
								project["join_time"] = ""
								project["application_time"] = ""
							else
								project["application_time"] = (project_user.application_time.nil? ? "" : project_user.application_time.strftime("%Y-%m-%d"))
								project["join_time"] = (project_user.join_time.nil? ? "" : project_user.join_time.strftime("%Y-%m-%d"))
							end
							project_list << project
						end
						result["projects"] = project_list
						result["count"] = project_count
						result["previous_page"] = ""
						if project_count > per_page
							result["next_page"] = "2"
						else
							result["next_page"] = ""
						end
						result
					else
						project_name = params[:project_name]
						project_count = Project.where("name LIKE ?  and publish_status = 1", "%#{project_name}%").count
						offset = (params[:page].to_i - 1) * per_page
						if project_count > offset
							projects = Project.where("name LIKE ?  and publish_status = 1", "%#{project_name}%").order(create_at: :desc).limit(per_page).offset(offset)
							project_list = []
							projects.each do |p|
								project = Hash.new
								project_user = ProjectUser.where(project_id: p.id, user_id: params[:user_id]).first
								project["id"] = p.id
								project["name"] = p.name
								project["score"] = p.score
								if project_user.nil?
									project["join_time"] = ""
									project["application_time"] = ""
								else
									project["application_time"] = (project_user.application_time.nil? ? "" : project_user.application_time.strftime("%Y-%m-%d"))
									project["join_time"] = (project_user.join_time.nil? ? "" : project_user.join_time.strftime("%Y-%m-%d"))
								end
								project_list << project
							end
							result["projects"] = project_list
							result["count"] = project_count
							if params[:page].to_i == 1
								result["previous_page"] = ""
							elsif  params[:page].nil?
								result["previous_page"] = ""
							else
								result["previous_page"] = (params[:page].to_i - 1).to_s
							end
							if project_count > (params[:page].to_i * per_page)
								result["next_page"] = "2"
							else
								result["next_page"] = ""
							end
							result
						else
							project_list = []
							result["projects"] = project_list
							result["count"] = project_count
							if params[:page].to_i == 1
								result["previous_page"] = ""
							else
								result["previous_page"] = (params[:page].to_i - 1).to_s
							end
							result["next_page"] = ""
						end
						result
					end
				else
					project = Project.where(id: params[:project_name], publish_status: 1).first
					project_user = ProjectUser.where(project_id: params[:project_name], user_id: params[:user_id]).first
					if project.nil?
						{code: "400", msg: "查询不到该项目或项目已关闭"}
					else
						project_list = []
						project_info = Hash.new
						project_info["id"] = project.id
						project_info["name"] = project.name
						project_info["score"] = project.score
						if project_user.nil?
							project_info["join_time"] = ""
							project_info["application_time"] = ""
						else
							project_info["application_time"] = (project_user.application_time.nil? ? "" : project_user.application_time.strftime("%Y-%m-%d"))
							project_info["join_time"] = (project_user.join_time.nil? ? "" : project_user.join_time.strftime("%Y-%m-%d"))
						end
						project_list  << project_info
						result["projects"] = project_list
						result["count"] = 1
						result["previous_page"] = ""
						result["next_page"] = ""
					  result
					end
				end
			end
		end

		desc '申请加入'
		params do
			optional :project_id, type: String, desc: '项目ID'
			optional :user_id, type: String, desc: '人员ID'
			optional :application_reason, type: String, desc: '申请理由'
		end
		post 'apply_join' do
			if params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			elsif params[:user_id].nil?
				{code: "400", msg: "缺少申请人ID"}
			elsif params[:application_reason].nil?
				{code: "400", msg: "请输入申请理由"}
			else
				project = Project.where(id: params[:project_id], publish_status: 1).first
				project_user = ProjectUser.where(project_id: params[:project_id], user_id: params[:user_id]).first
				if project.nil?
					{code: "400", msg: "没有查询到项目或项目已关闭"}
				else
					if project_user.nil?
						project_user = ProjectUser.new
						project_user.project_id = params[:project_id]
						project_user.user_id = params[:user_id]
						project_user.role = 3
						project_user.application_reason = params[:application_reason]
						project_user.application_time = Time.new
						# user = User.find(params[:user_id])
						if project_user.save
							# MessageApi.new_message(params[:user_id], 1, 1, -99, params[:project_id], "加入项目申请", user.name+"申请加入"+project.name+"项目，请审批")
							{code: "0", msg: "OK"}
						else
							{code: "400", msg: "申请失败"}
						end
					else
						{code: "400", msg: "已加入或申请中"}
					end
				end
			end
		end

		desc '获取项目申请列表'
		params do
			optional :user_id, type: String, desc: '人员ID'
			optional :project_id, type: String, desc: '项目申请ID'
			optional :page, type: String, desc: '项目申请ID'
		end
		get 'project_apply_list' do
			if params[:user_id].nil? || params[:project_id].nil?
				{code: "400", msg: "缺少参数"}
			else
				if params[:page].nil?
					page = 1
				else
					page = params[:page]
				end
				project_users = ProjectUser.where("project_id = ? and join_time IS NULL AND application_time IS NOT NULL", params[:project_id]).paginate(:page => page, :per_page => 999999).order('application_time DESC')				
				project_user_list = []
				project_users.each do |pu|
					user = User.find(pu.user_id)
					project_user = Hash.new
					h_projects = ProjectUser.where("user_id = ? and join_time IS NOT NULL", pu.user_id)
					history_projects = ""
					h_projects.each do |hp|
						project = Project.find(hp.project_id)				
						content = "工作于：" + project.name + "," + "加入时间：" + hp.join_time.strftime("%Y-%m-%d %H:%M:%S") + ",离职时间：" + (hp.exit_time.nil? ? "在职" : hp.exit_time.strftime("%Y-%m-%d %H:%M:%S"))
						
						if history_projects.length == 0
							history_projects = content
						else
							history_projects = history_projects + ";" + content
						end 
					end

					project_user["id"] = pu.id
					project_user["name"] = user.name
					project_user["application_reason"] = pu.application_reason.nil? ? "" : pu.application_reason
					project_user["application_time"] = pu.application_time.strftime("%Y-%m-%d %H:%M:%S")
					project_user["history_projects"] = history_projects
					project_user_list << project_user
				end
				count = project_users.size
				next_page = params[:page].to_i + 1
				{
					code: "0", 
					msg: "OK",
					project_apply: project_user_list,
					count: count,
					next_page: next_page
				}
			end
		end

		desc '处理项目申请'
		params do
			optional :user_id, type: String, desc: '人员ID'
			optional :apply_id, type: String, desc: '项目申请ID'
			optional :deal_status, type: String, desc: '处理状态 1. 通过'
		end
		post 'deal_join' do
			if params[:user_id].nil? || params[:apply_id].nil? || params[:deal_status].nil?
				{code: "400", msg: "缺少参数"}
			else
				project_user = ProjectUser.find(params[:apply_id])
				project_user.join_time = Time.now
				project_user.save
				project = Project.find(project_user.project_id)
				user_detail = UserDetail.where(user_id: params[:user_id]).first
				if user_detail.company_id.nil?
					project_team = ProjectTeam.where("project_id = ? and company_id = ?", project_user.project_id, project.certification_company).first
					if project_team.nil?
						
					else
						project_user.upload_project_team = project_team.id
					end
				else
					project_team = ProjectTeam.where("project_id = ? and company_id = ?", project_user.project_id, user_detail.company_id).first
					company = Company.find(user_detail.company_id)
					if project_team.nil?
						project_team = ProjectTeam.new
						project_team.team_name = company.company_name + "默认项目班组"
						project_team.company_id = company.id
						project_team.project_id =  project_user.project_id
						project_team.team_code = rand(999999).to_s
						project_team.save
						project_user.upload_project_team = project_team.id
						project_user.save
					else
						project_user.upload_project_team = project_team.id
						project_user.save
					end

				end
				
				ProjectApi::make_project_user(project_user.project_id)
				{code: "0", msg: "OK"}
			end
		end
		desc '人员删除'
		params do
			optional :user_id, type: String, desc: '人员ID'
			optional :project_id, type: String, desc: '项目ID'
		end
		post 'delete_project_user' do
			if params[:user_id].nil? || params[:project_id].nil?
				{code: "400", msg: "缺少参数"}
			else
				project_user = ProjectUser.where(user_id: params[:user_id], project_id: params[:project_id]).first
				project_user.exit_time = Time.now
				project_user.save
				ProjectApi::make_project_user(project_user.project_id)
				{code: "0", msg: "OK"}
			end
		end
		########><><><><><><><><><><><>项目公告相关接口<><><><><><><><><><>#############

		# 项目公告列表接口(管理员和项目经理看到的一样)-要调整
		# 接口编号:2.3.7
		# 参数:
		## project_id type:String  项目ID
		## user_id    type:String  用户ID
		## page       type:Integer 页码
		# 请求方式: get
		# 返回: 公告列表
		desc '项目公告列表'
		params do
			optional :project_id, type: String, desc: '项目ID'
			optional :user_id, type: String, desc: '人员ID'
			optional :page, type: Integer, desc: '页码'
		end
		get 'notices' do
			puts "1xxxx2323"
			per_page = 10
			if params[:project_id].nil?
				{code: "400", msg: "请输入项目ID"}
			elsif params[:user_id].nil?
				{code: "400", msg: "请输入人员ID"}
			else
				result = Hash.new
				result["code"] = "0"
				result["msg"] = "OK"
				notices_list = []
				puts "12323"
				if params[:page].nil? || params[:page] == 1
					puts "123dasf23"
					project = Project.where(id: params[:project_id], publish_status: 1).first
					if project.nil?
						{code: "400", msg: "项目ID不正确或项目已关闭"}
					else
						puts "123"
						project_manager_master = ProjectUser.where("project_id = ? AND user_id = ? AND join_time IS NOT NULL AND exit_time IS NULL AND role = 1", params[:project_id], params[:user_id]).first
						if !(project_manager_master.nil?)
							notices = Notice.where("project_id = ? and verified_type != 3", params[:project_id]).order(update_at: :desc).limit per_page
							notices_count = Notice.where("project_id = ? and verified_type != 3", params[:project_id]).count
							result["count"] = notices_count
							notices.each do |n|
								notice = Hash.new
								notice["id"] = n.id
								notice["content"] = (n.content.nil? ? "" : n.content[0,100])
								notice["verified_type"] = n.verified_type
								notice["publish_time"] = n.create_at.nil?? "" : CommonApi::format_time(n.create_at)
								notices_list << notice
							end
							result["notices"] = notices_list
							result["count"] = notices_count
							result["previous_page"] = ""
							if notices_count > per_page
								result["next_page"] = "2"
							else
								result["next_page"] = ""
							end
							result
						else
							project_user = ProjectUser.where("project_id = ? AND user_id = ? AND join_time IS NOT NULL AND exit_time IS NULL", params[:project_id], params[:user_id]).first
							if project_user.nil?
								{code: "400", msg: "没有权限查看该项目公告"}
							else
								notices = Notice.where("project_id = ? and (verified_type = 2 or user_id = ?) ", params[:project_id], params[:user_id]).order(update_at: :desc).limit per_page
								notices_count = Notice.where("project_id = ? and (verified_type = 2 or user_id = ?)", params[:project_id], params[:user_id]).count
								result["count"] = notices_count
								notices.each do |n|
									notice = Hash.new
									notice["id"] = n.id
									notice["content"] = (n.content.nil? ? "" : n.content[0,100])
									notice["verified_type"] = n.verified_type
									notice["publish_time"] = n.create_at.nil?? "" : CommonApi::format_time(n.create_at)
									notices_list << notice
								end
								result["notices"] = notices_list
								result["count"] = notices_count
								result["previous_page"] = ""
								if notices_count > per_page
									result["next_page"] = "2"
								else
									result["next_page"] = ""
								end
								result
							end
						end
					end
				else
					project = Project.where(id: params[:project_id], publish_status: 1).first
					if project.nil?
						{code: "400", msg: "项目ID不正确或项目已关闭"}
					else
						project_manager_master = ProjectUser.where("project_id = ? AND user_id = ? AND join_time IS NOT NULL AND exit_time IS NULL AND role = 1", params[:project_id], params[:user_id]).first
						if !(project_manager_master.nil?)
							offset = (params[:page].to_i - 1) * per_page
							notices = Notice.where("project_id = ? and verified_type != 3", params[:project_id]).order(update_at: :desc).limit(per_page).offset(offset)
							page_count = Notice.where("project_id = ? and verified_type != 3", params[:project_id]).count
							result["count"] = page_count
							notices.each do |n|
								notice = Hash.new
								notice["id"] = n.id
								notice["content"] = (n.content.nil? ? "" : n.content[0,100])
								notice["verified_type"] = n.verified_type
								notice["publish_time"] = n.create_at.nil?? "" : CommonApi::format_time(n.create_at)
								notices_list << notice
							end
							result["notices"] = notices_list
							result["count"] = page_count
							result["previous_page"] = (params[:page].to_i - 1).to_s
							if page_count > (params[:page].to_i * per_page)
								result["next_page"] = params[:page].to_i + 1
							else
								result["next_page"] = ""
							end
							result
						else
							project_user = ProjectUser.where("project_id = ? AND user_id = ? AND join_time IS NOT NULL AND exit_time IS NULL", params[:project_id], params[:user_id]).first
							if project_user.nil?
								{code: "400", msg: "没有权限查看该项目公告"}
							else
								notices = Notice.where("project_id = ? and (verified_type = 2 or user_id = ?) ", params[:project_id], params[:user_id]).order(update_at: :desc).limit per_page
								page_count = Notice.where("project_id = ? and (verified_type = 2 or user_id = ?) ", params[:project_id], params[:user_id]).count
								result["count"] = page_count
								notices.each do |n|
									notice = Hash.new
									notice["id"] = n.id
									notice["content"] = (n.content.nil? ? "" : n.content[0,100])
									notice["verified_type"] = n.verified_type
									notice["publish_time"] = n.create_at.nil?? "" : CommonApi::format_time(n.create_at)
									notices_list << notice
								end
								result["notices"] = notices_list
								result["count"] = notices_count
								result["previous_page"] = (params[:page].to_i - 1).to_s
								if page_count > (params[:page].to_i * per_page)
									result["next_page"] = params[:page].to_i + 1
								else
									result["next_page"] = ""
								end
								result
							end
						end
					end
				end
			end
		end

		# 项目公告详情接口
		# 接口编号:2.3.8
		# 参数:
		## id type:String  公告ID
		# 请求方式: get
		# 返回: 公告详情
		desc '项目公告详情'
		params do
			optional :id, type: String, desc: '公告ID'
		end
		get 'notice' do
			if params[:id].nil?
				{code: "400", msg: "请输入公告id"}
			else
				result = Hash.new
				notice = Notice.where(id: params[:id]).first
				if notice.nil?
					result["code"] = "400"
					result["msg"] = "公告不存在"
				else
					notice_info = Hash.new
					result["code"] = "0"
					result["msg"] = "OK"
					notice_info["id"] = notice.id
					notice_info["content"] = (notice.content.nil? ? "" : notice.content)
					notice_info["verified_type"] = notice.verified_type
					notice_info["publish_time"] = notice.create_at.nil?? "" : CommonApi::format_time(notice.create_at)
					result["notice"] = notice_info
				end
				result
			end
		end

		# 新增项目公告接口
		# 接口编号:2.3.9
		# 参数:
		## project_id  项目ID
		## user_id  发布人ID
		## content  公告内容
		# 请求方式: post
		# 返回: 是否成功过
		desc '新增公告'
		params do
			optional :project_id, type: String, desc: '项目ID'
			optional :user_id, type: String, desc: '发布人ID'
			optional :content, type: String, desc: '公告内容'
		end
		post 'add_notice' do
			if params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			elsif params[:user_id].nil?
				{code: "400", msg: "缺少发布人ID"}
			elsif params[:content].nil?
				{code: "400", msg: "请输入发布内容"}
			else
				project_user = ProjectUser.where("project_id = ? AND user_id = ? AND join_time IS NOT NULL AND exit_time IS NULL", params[:project_id], params[:user_id]).first
				project = Project.where(id: params[:project_id], publish_status: 1).first
				user = User.find(project_user.user_id)
				if project_user.nil? || project_user.role == 2 
					{code: "400", msg: "您没有发布公告权限"}
				elsif project.nil?
					{code: "400", msg: "项目已关闭"}
				else
					notice = Notice.new
					notice.user_id = params[:user_id]
					notice.project_id = params[:project_id]
					notice.content = params[:content]
					notice.create_at = Time.now
					notice.update_at = Time.now
					if project_user.role == 1
						notice.verified_type = 2
						project_users = ProjectUser.where("project_id = ? AND join_time IS NOT NULL AND exit_time IS NULL", params[:project_id])
						if notice.save
							project_users.each do |pu|
								MessageApi.new_message2(pu.user_id, 2, 6, notice.id, notice.project_id, "项目公告", project.name+"项目有一条新的项目公告", "", user.name)
							end
							{code: "0", msg: "OK", notice_id: notice.id}
						else
							{code: "400", msg: "保存失败"}
						end
						
					else
						notice.verified_type = 1
						if notice.save
							project_users = ProjectUser.where("project_id = ? AND join_time IS NOT NULL AND exit_time IS NULL", params[:project_id])
							project_users.each do |pu|
								if pu.role == 1
									MessageApi.new_message2(pu.user_id, 2, 6, notice.id, params[:project_id], "项目公告审核", user.name+"在"+project.name+"项目发布一条公告，请审核。", "", user.name)
								end
							end
							{code: "0", msg: "OK", notice_id: notice.id}
						else
							{code: "400", msg: "保存失败"}
						end			
					end					
				end
			end
		end

		# 审批项目公告接口
		# 接口编号:2.3.10
		# 参数:
		## project_id  项目ID
		## user_id  发布人ID
		## content  公告内容
		# 请求方式: post
		# 返回: 是否成功过
		desc '审批公告'
		params do
			optional :notice_id, type: String, desc: '公告ID'
			optional :verified_type, type: Integer, desc: '审核状态'
			optional :user_name, type: String, desc: '用户名'
			optional :passwd, type: String, desc: '密码'
		end
		post 'verified_notice' do
			if params[:notice_id].nil?
				{code: "400", msg: "缺少公告ID"}
			elsif params[:verified_type].nil?
				{code: "400", msg: "请选择审批意见"}
			elsif params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "缺少用户名和密码"}
			else
				notice = Notice.where(id: params[:notice_id]).first
				if notice.nil?
					{code: "400", msg: "查询不到可审核的公告"}
				else
					user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
					if user_id == -1
						{code: "400", msg: "用户名和密码错误"}
					else
						project = Project.where(id: notice.project_id, publish_status: 1).first
						project_manager_master = ProjectUser.where(project_id: notice.project_id, role: 1, user_id: user_id).first
						if project.nil? || project_manager_master.nil?
							{code: "400", msg: "只有项目经理可以审批公告或项目已关闭"}
						else
							if params[:verified_type].to_i == 2 || params[:verified_type].to_i == 3
								notice.verified_type = params[:verified_type].to_i
								notice.update_at = Time.now
								if notice.save
									if notice.verified_type.to_i == 2
										content = "您在"+project.name+"项目发布的公告已被通过。"
										MessageApi.new_message2(notice.user_id, 2, 6, notice.id, notice.project_id, "项目公告审核反馈", content, "", user.name)
										project_users = ProjectUser.where(project_id: project.id)
										publish_user = User.find(notice.user_id)
										project_users.each do |pu|
											MessageApi.new_message2(pu.user_id, 2, 6, notice.id, notice.project_id, "项目公告", project.name+"项目有一条新的项目公告", "", publish_user.name)
										end
										{code: "0", msg: "OK"}
									else
										content = "您在"+project.name+"项目发布的公告已被拒绝。"
										MessageApi.new_message2(notice.user_id, 2, 6, notice.id, notice.project_id, "项目公告审核反馈", content, "", user.name)
									end
								else
									{code: "400", msg: "审核失败"}
								end
							else
								{code: "400", msg: "只可设置通过或不通过"}
							end
						end
					end
				end
			end
		end

		########><><><><><><><><><><><>项目签到相关接口<><><><><><><><><><>#############

		# 签到接口
		# 接口编号:2.5.1
		# 参数:
		## project_id type:String  项目ID
		## user_id    type:String  用户ID
		## lat        type:Double  维度
		## lng				type:Double	 经度
		# 请求方式: POST
		# 返回: 是否成功
		desc '签到接口'
		params do
			optional :project_id, type: String, desc: "项目ID"
			optional :user_name, type: String,  desc: "用户名"
			optional :passwd, type: String,  desc: "用户密码"
			optional :lat, type: BigDecimal, desc: "维度"
			optional :lng, type: BigDecimal, desc: "经度"
		end
		post 'signed' do
			if params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			elsif params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "缺少用户名和密码参数"}
			elsif params[:lat].nil? || params[:lng].nil?
				{code: "400", msg: "缺少签到地理位置"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					project = Project.where(id: params[:project_id], publish_status: 1).first
					project_user = ProjectUser.where(project_id: params[:project_id], user_id: user_id).first
					if project.nil? || project_user.nil?
						{code: "400", msg: "项目已关闭或你不是项目成员"}
					else
						if project.lat.nil? || project.lng.nil?
							{code: "400", msg: "签到区域未维护"}
						else
							time = Time.new
							year = time.year
							month = time.month
							day = time.day
							sign_detail = Sign.where(project_id: params[:project_id], user_id: user_id, year: year, month: month, day: day).first
							if sign_detail.nil?
								distances = CommonApi.calculated_latitude(project.lat, project.lng, params[:lat], params[:lng])
								if distances <= project.area_radius.to_i
									sign = Sign.new
									sign.user_id = user_id
									sign.project_id = params[:project_id]
									sign.lat = params[:lat]
									sign.lng = params[:lng]
									sign.year = year
									sign.month = month
									sign.day = day
									sign.create_at = Time.now
									sign.update_at = Time.now
									if sign.save
										sign_detail = SignDetail.where(project: params[:project_id], year: year, month: month, day: day, user_id: user_id).first
										if sign_detail.nil?
										else
											sign_detail.sign_id = sign.id
											sign_detail.save
										end
										{code: "0", msg: "OK"}
									else
										{code: "400", msg: "签到失败"}
									end
								else
									{code: "400", msg: "不在可签到区域"}
								end
							else
								sign_detail.update_at = Time.now
								sign_detail.save
								{code: "0", msg: "OK"}
							end
						end
					end
				end
			end
		end

		# 个人签到记录接口
		# 接口编号:2.5.2
		# 参数:
		## project_id type:String  项目ID
		## user_id    type:String  用户ID
		## year        type:Integer  年
		## month				type:Integer	 月
		# 请求方式: POST
		# 返回: 是否成功
		desc '个人签到记录查询'
		params do
			optional :project_id, type: String, desc: "项目ID"
			optional :user_id, type: String,  desc: "用户ID"
			optional :year, type: Integer, desc: "年"
			optional :month, type: Integer, desc: "月"
		end
		get 'sign_record' do
			if params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			elsif params[:user_id].nil?
				{code: "400", msg: "缺少签到人ID"}
			else
				if params[:year].nil? && params[:month].nil?
					time = Time.new
					year = time.year
					month = time.month
					result = ProjectApi::get_sign(year, month, params[:project_id], params[:user_id])
					result
				elsif params[:year].nil? || params[:month].nil?
					{code: "400", msg: "年和月必须组合提交"}
				else
					result = ProjectApi::get_sign(params[:year].to_i, params[:month].to_i, params[:project_id], params[:user_id])
					result
				end
			end
		end

		# 管理员设置签到区域接口
		# 接口编号:2.5.2
		# 参数:
		## project_id type:String  项目ID
		## user_name    type:String  用户ID
		## lat        type:Double  维度
		## lng				type:Double	 经度
		# 请求方式: POST
		# 返回: 是否成功
		desc '管理员设置签到区域接口'
		params do
			optional :project_id, type: String, desc: "项目ID"
			optional :user_name, type: String,  desc: "用户名"
			optional :passwd, type: String,  desc: "用户密码"
			optional :lat, type: BigDecimal, desc: "维度"
			optional :lng, type: BigDecimal, desc: "经度"
			optional :area_radius, type:BigDecimal, desc: "范围"
		end
		post 'sign_area' do
			if params[:project_id].nil?
				{code: "400", msg: "缺少项目ID参数"}
			elsif params[:lat].nil? || params[:lng].nil?
				{code: "400", msg: "缺少签到地理位置"}
			elsif params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "缺少用户名和密码参数"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					project = Project.where(id: params[:project_id], publish_status: 1).first
					project_user = ProjectUser.where(user_id: user_id, role: 1, project_id: params[:project_id]).first
					if project.nil?  || project_user.nil?
						{code: "400", msg: "项目已关闭或你不是管理员"}
					else
						project.lat = params[:lat]
						project.lng = params[:lng]
						project.area_radius = params[:area_radius]
						if project.save
							{code: "0", msg: "OK"}
						else
							{code: "400", msg: "保存失败"}
						end
					end
				end
			end
		end

		########><><><><><><><><><><><>项目工作相关接口<><><><><><><><><><>#############

		# 新建安排工作接口
		# 接口编号:2.6.1
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## project_id type:String  项目ID
		## work       type:String  工作内容
		## user_list  type:Integer 相关人员列表
		# 请求方式: post
		# 返回: 工作ID
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd, type: String, desc: '密码'
			optional :project_id, type: String, desc: '项目ID'
			optional :work, type: String, desc: '工作内容'
			optional :user_list, type: String, desc: '相关人员列表'
		end
		post 'arrange_work' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:user_list].nil?
				{code: "400", msg: "缺少项目相关人员"}
			elsif params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			elsif params[:work].nil?
				{code: "400", msg: "缺少工作内容"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名密码错误"}
				else
					project_user = ProjectUser.where(project_id: params[:project_id], user_id: user_id).first
					project = Project.where(id: params[:project_id], publish_status: 1).first
					if project_user.nil? || project_user.role == 2
						{code: "400", msg: "不是项目内部人员"}
					elsif project.nil?
						{code: "400", msg: "项目已关闭"}
					else	
						work = Work.new
						work.project_id = params[:project_id]
						work.content = params[:work]
						work.publish_user = user_id
						work.work_type = 2
						work.verified_type = 2
						work.created_at = Time.now
						work.updated_at = Time.now
						if work.save
							ProjectApi.create_work_user(user_id, params[:project_id], work.id, 2, 2, 2)   # 新增一条发布人关系
							user = User.find(user_id)
							project = Project.find(params[:project_id])
							((params[:user_list])[1..-2].split(",")).each do |ul|
								ProjectApi.create_work_user(ul.to_i, params[:project_id], work.id, 2, 3, 2)
								MessageApi.new_message2(ul.to_i, 2, 3, work.id, params[:project_id], "工作安排", user.name+"在"+project.name+"项目给您发布了一条工作安排。", "", user.name)
							end
							{code: "0", msg: "OK"}
						else
							{code: "400", msg: "保存失败"}
						end
					end
				end
			end
		end

		# 新建申请工作接口
		# 接口编号:2.6.2
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## project_id type:String  项目ID
		## work       type:String  工作内容
		## verified_user type:String 审核人
		## user_list  type:Integer 相关人员列表
		# 请求方式: post
		# 返回: 工作ID
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd, type: String, desc: '密码'
			optional :project_id, type: String, desc: '项目ID'
			optional :work, type: String, desc: '工作内容'
			optional :verified_user, type: String, desc: '审核人'
			optional :user_list, type: String, desc: '相关人员列表'
		end
		post 'apply_work' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:user_list].nil?
				{code: "400", msg: "缺少项目相关人员"}
			elsif params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			elsif params[:verified_user].nil?
				{code: "400", msg: "缺少审核人"}
			elsif params[:work].nil?
				{code: "400", msg: "缺少工作内容"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名密码错误"}
				else
					project_user = ProjectUser.where(project_id: params[:project_id], user_id: user_id).first
					project = Project.where(id: params[:project_id], publish_status: 1).first
					if project_user.nil? || project_user.role == 2
						{code: "400", msg: "不是项目内部人员或是监视员"}
					elsif project.nil?
						{code: "400", msg: "项目已关闭"}					
					else
						user = User.find(user_id)
						project = Project.find(params[:project_id])
						work = Work.new
						work.project_id = params[:project_id]
						work.content = params[:work]
						work.publish_user = user_id
						work.work_type = 1
						work.verified_type = 1
						work.verified_user = params[:verified_user]
						work.created_at = Time.now
						work.updated_at = Time.now
						if work.save
							ProjectApi.create_work_user(user_id, params[:project_id], work.id, 1, 2, 1)   # 新增一条发布人关系
							ProjectApi.create_work_user(params[:verified_user], params[:project_id], work.id, 1, 1, 1)		# 新增一条审核人关系
							MessageApi.new_message2(params[:verified_user], 2, 4, work.id, params[:project_id], "工作申请审核", user.name+"在"+project.name+"项目发布一条工作申请，请审核。", "", user.name)
							((params[:user_list])[1..-2].split(",")).each do |ul|
								ProjectApi.create_work_user(ul.to_i, params[:project_id], work.id, 1, 3, 1)
							end
							{code: "0", msg: "OK"}
						else
							{code: "400", msg: "保存失败"}
						end
					end
				end
			end
		end


		# 工作申请列表接口
		# 接口编号:2.6.3
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## project_id    type:String  工作ID
		## page       type:Integer  页码
		# 请求方式: get
		# 返回: 工作ID
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd, type: String, desc: '密码'
			optional :project_id, type: String, desc: '项目ID'
			optional :page, type: Integer, desc: '工作内容'
		end
		get 'workapl_list' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					per_page = 10
					project = Project.where(id: params[:project_id], publish_status: 1).first
					project_user = ProjectUser.where(project_id: params[:project_id], user_id: user_id).first
					if project.nil? || (project_user.nil? && !(UserAPI.auth_user(user_id) == -99))
						{code: "400", msg: "项目不存在或你不是项目成员"}
					else
						result = Hash.new
						result["code"] = "0"
						result["msg"] = "OK"
						if params[:page].nil? || params[:page] == 1
							if project.project_manager == user_id 
								work_users = WorkUser.where("((user_id = ? and work_type = 1 and user_type = 1) or (user_id = ? and work_type = 1 and user_type = 2 ) or (user_id = ? and work_type = 1 and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).limit per_page
								work_count = WorkUser.where("((user_id = ? and work_type = 1 and user_type = 1) or (user_id = ? and work_type = 1 and user_type = 2 ) or (user_id = ? and work_type = 1 and user_type = 3 and confirm_status != 1 )) AND project_id = ?", user_id, user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).count
								result["works"] = ProjectApi.build_work(work_users)
								result["count"] = work_count.length
								result["previous_page"] = ""
								if (work_count.length) > per_page 
									result["next_page"] = "2"
								else
									result["next_page"] = ""
								end
								result
							elsif  UserAPI.auth_user(user_id) == -99								
								work_users = Work.where("project_id = ? and work_type = 1 ", params[:project_id]).order(updated_at: :desc).limit per_page
								work_count =Work.where("project_id = ?  and work_type = 1 ", params[:project_id]).order(updated_at: :desc).count
								result["code"] = "0"
								result["msg"] = "OK"
								result["works"] = ProjectApi.build_admin_work(work_users)
								result["count"] = work_count
								result["previous_page"] = ""
								if (work_count) > per_page
									result["next_page"] = "2"
								else
									result["next_page"] = ""
								end
								result
							else
								work_users = WorkUser.where("((user_id = ? and work_type = 1 and user_type = 2 ) or (user_id = ? and work_type = 1 and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).limit per_page
								work_count = WorkUser.where("((user_id = ? and work_type = 1 and user_type = 2 ) or (user_id = ? and work_type = 1 and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).count
								result["code"] = "0"
								result["msg"] = "OK"
								result["works"] = ProjectApi.build_work(work_users)
								result["count"] = work_count.length
								result["previous_page"] = ""
								if (work_count.length) > per_page
									result["next_page"] = "2"
								else
									result["next_page"] = ""
								end
								result
							end
						else
							if project.project_manager == user_id 
								offset = (params[:page].to_i - 1) * per_page
								work_users = WorkUser.where("((user_id = ? and work_type = 1 and user_type = 1) or (user_id = ? and work_type = 1 and user_type = 2 ) or (user_id = ? and work_type = 1 and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).limit(per_page).offset(offset)
								work_count = WorkUser.where("((user_id = ? and work_type = 1 and user_type = 1) or (user_id = ? and work_type = 1 and user_type = 2 ) or (user_id = ? and work_type = 1 and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).offset(offset).count
								result["works"] = ProjectApi.build_work(work_users)
								result["count"] = work_count.length
								result["previous_page"] = (params[:page].to_i - 1).to_s
								if (work_count.length) > (params[:page].to_i * per_page)
									result["next_page"] = params[:page].to_i + 1
								else
									result["next_page"] = ""
								end
								result
							elsif  UserAPI.auth_user(user_id) == -99
								work_users = Work.where("project_id = ?  ADN work_type = 1 ", params[:project_id]).order(updated_at: :desc).limit(per_page).offset(offset)
								work_count =Work.where("project_id = ?  ADN work_type = 1 ", params[:project_id]).order(updated_at: :desc).count
								result["code"] = "0"
								result["msg"] = "OK"
								result["works"] = ProjectApi.build_admin_work(work_users)
								result["count"] = work_count
								result["previous_page"] = (params[:page].to_i - 1).to_s
								if (work_count) > (params[:page].to_i * per_page)
									result["next_page"] = params[:page].to_i + 1
								else
									result["next_page"] = ""
								end
								result
							else
								work_users = WorkUser.where("((user_id = ? and work_type = 1 and user_type = 2 ) or (user_id = ? and work_type = 1 and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).limit(per_page).offset(offset)
								work_count = WorkUser.where("((user_id = ? and work_type = 1 and user_type = 2 ) or (user_id = ? and work_type = 1 and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).offset(offset).count
								result["works"] = ProjectApi.build_work(work_users)
								result["count"] = work_count.length
								result["previous_page"] = (params[:page].to_i - 1).to_s
								if (work_count.length) > (params[:page].to_i * per_page)
									result["next_page"] = params[:page].to_i + 1
								else
									result["next_page"] = ""
								end
								result
							end
						end
					end
				end
			end
		end

		# 工作安排列表接口
		# 接口编号:2.6.4
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## project_id    type:String  工作ID
		## page       type:Integer  页码
		# 请求方式: get
		# 返回: 工作ID
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd, type: String, desc: '密码'
			optional :project_id, type: String, desc: '项目ID'
			optional :page, type: Integer, desc: '工作内容'
		end
		get 'workarg_list' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					per_page = 10
					project = Project.where(id: params[:project_id], publish_status: 1).first
					project_user = ProjectUser.where(project_id: params[:project_id], user_id: user_id).first
					if project.nil? || (project_user.nil? && !(UserAPI.auth_user(user_id) == -99))
						{code: "400", msg: "项目不存在或你不是项目成员"}
					else
						result = Hash.new
						result["code"] = "0"
						result["msg"] = "OK"
						if params[:page].nil? || params[:page] == 1
							if UserAPI.auth_user(user_id) == -99
								work_users = Work.where("project_id = ? and work_type = 2 ", params[:project_id]).order(updated_at: :desc).limit per_page
								work_count =Work.where("project_id = ?  and work_type = 2 ", params[:project_id]).order(updated_at: :desc).count
								result["code"] = "0"
								result["msg"] = "OK"
								result["works"] = ProjectApi.build_admin_work(work_users)
								result["count"] = work_count
								result["previous_page"] = ""
								if (work_count) > per_page
									result["next_page"] = "2"
								else
									result["next_page"] = ""
								end
								result
							else
								work_users = WorkUser.where("((user_id = ? and work_type = 2 and user_type = 2 ) or (user_id = ? and work_type = 2 and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).limit per_page
								work_count = WorkUser.where("((user_id = ? and work_type = 2 and user_type = 2 ) or (user_id = ? and work_type = 2 and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).count
								result["works"] = ProjectApi.build_work(work_users)
								result["count"] = work_count.length
								result["previous_page"] = ""
								if (work_count.length) > per_page
									result["next_page"] = "2"
								else
									result["next_page"] = ""
								end
								result
							end
						else
							if UserAPI.auth_user(user_id) == -99
								work_users = Work.where("project_id = ?  ADN work_type = 2 ", params[:project_id]).order(updated_at: :desc).limit(per_page).offset(offset)
								work_count =Work.where("project_id = ?  ADN work_type = 2 ", params[:project_id]).order(updated_at: :desc).count
								result["code"] = "0"
								result["msg"] = "OK"
								result["works"] = ProjectApi.build_admin_work(work_users)
								result["count"] = work_count
								result["previous_page"] = (params[:page].to_i - 1).to_s
								if (work_count) > (params[:page].to_i * per_page)
									result["next_page"] = params[:page].to_i + 1
								else
									result["next_page"] = ""
								end
								result
							else
								offset = (params[:page].to_i - 1) * per_page
								work_users = WorkUser.where("((user_id = ? and work_type = 2 and user_type = 2 ) or (user_id = ? and work_type = 2 and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).limit(per_page).offset(offset)
								work_count = WorkUser.where("((user_id = ? and work_type = 2 and user_type = 2 ) or (user_id = ? and work_type = 2 and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).offset(offset).count
				
								result["works"] = ProjectApi.build_work(work_users)
								result["count"] = work_count.length
								result["previous_page"] = (params[:page].to_i - 1).to_s
								if (work_count.length) > (params[:page].to_i * per_page)
									result["next_page"] = params[:page].to_i + 1
								else
									result["next_page"] = ""
								end
								result
							end
						end
					end
				end
			end
		end

		# 我的工作接口
		# 接口编号:2.6.5
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## project_id    type:String  工作ID
		## page       type:Integer  页码
		# 请求方式: get
		# 返回: 工作ID
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd, type: String, desc: '密码'
			optional :project_id, type: String, desc: '项目ID'
			optional :page, type: Integer, desc: '工作内容'
		end
		get 'my_works' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					per_page = 10
					project = Project.where(id: params[:project_id], publish_status: 1).first
					project_user = ProjectUser.where(project_id: params[:project_id], user_id: user_id).first
					if project.nil? || (project_user.nil? && !(UserAPI.auth_user(user_id) == -99))
						{code: "400", msg: "项目不存在或你不是项目成员"}
					else
						result = Hash.new
						result["code"] = "0"
						result["msg"] = "OK"
						if params[:page].nil? || params[:page] == 1
							work_users = WorkUser.where("((user_id = ? and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).limit(per_page)
							work_count = WorkUser.where("((user_id = ? and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).count
							result["works"] = ProjectApi.build_work(work_users)
							result["count"] = work_count.length
							result["previous_page"] = ""
							if (work_count.length) > per_page
								result["next_page"] = "2"
							else
								result["next_page"] = ""
							end
							result
						else
							offset = (params[:page].to_i - 1) * per_page
							work_users = WorkUser.where("((user_id = ? and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).limit(per_page).offset(offset)
							work_count = WorkUser.where("((user_id = ? and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).offset(offset).count
							result["works"] = ProjectApi.build_work(work_users)
							result["count"] = work_count.length
							result["previous_page"] = (params[:page].to_i - 1).to_s
							if (work_count.length) > (params[:page].to_i * per_page)
								result["next_page"] = params[:page].to_i + 1
							else
								result["next_page"] = ""
							end
							result
						end
					end
				end
			end
		end

		# 全部工作接口
		# 接口编号:2.6.6
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## project_id    type:String  工作ID
		## page       type:Integer  页码
		# 请求方式: get
		# 返回: 工作ID
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd, type: String, desc: '密码'
			optional :project_id, type: String, desc: '项目ID'
			optional :page, type: Integer, desc: '工作内容'
		end
		get 'all_works' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					per_page = 10
					project = Project.where(id: params[:project_id], publish_status: 1).first
					project_user = ProjectUser.where(project_id: params[:project_id], user_id: user_id).first
					if project.nil? || (project_user.nil? && !(UserAPI.auth_user(user_id) == -99))
						{code: "400", msg: "项目不存在或你不是项目成员"}
					else
						result = Hash.new
						result["code"] = "0"
						result["msg"] = "OK"
						if params[:page].nil? || params[:page] == 1
							if UserAPI.auth_user(user_id) == -99
								work_users = Work.where("project_id = ?", params[:project_id]).order(updated_at: :desc).limit per_page
								work_count =Work.where("project_id = ?", params[:project_id]).order(updated_at: :desc).count
								result["code"] = "0"
								result["msg"] = "OK"
								result["works"] = ProjectApi.build_admin_work(work_users)
								result["count"] = work_count
								result["previous_page"] = ""
								if (work_count) > per_page
									result["next_page"] = "2"
								else
									result["next_page"] = ""
								end
								result
							else
								work_users = WorkUser.where("((user_id = ?  and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 ) or (confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).limit(per_page)
								work_count = WorkUser.where("((user_id = ?  and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 ) or (confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("work_id").count
								result["works"] =ProjectApi.build_work(work_users)
								result["count"] = work_count.length
								result["previous_page"] = ""
								if (work_count.length) > per_page
									result["next_page"] = "2"
								else
									result["next_page"] = ""
								end
								result
							end
						else
							if UserAPI.auth_user(user_id) == -99
								work_users = Work.where("project_id = ? ", params[:project_id]).order(updated_at: :desc).limit(per_page).offset(offset)
								work_count =Work.where("project_id = ? ", params[:project_id]).order(updated_at: :desc).count
								result["code"] = "0"
								result["msg"] = "OK"
								result["works"] = ProjectApi.build_admin_work(work_users)
								result["count"] = work_count
								result["previous_page"] = (params[:page].to_i - 1).to_s
								if (work_count) > (params[:page].to_i * per_page)
									result["next_page"] = params[:page].to_i + 1
								else
									result["next_page"] = ""
								end
								result
							else
								offset = (params[:page].to_i - 1) * per_page
								work_users = WorkUser.where("((user_id = ?  and user_type = 1) 
							 	or (user_id = ? and user_type = 2 )
							 	or (user_id = ? and user_type = 3 and confirm_status != 1 )
							 	or (confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).limit(per_page).offset(offset)
								work_count = WorkUser.where("((user_id = ?  and user_type = 1) 
							 	or (user_id = ? and user_type = 2 )
							 	or (user_id = ? and user_type = 3 and confirm_status != 1 )
							 	or (confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("work_id").order(updated_at: :desc).offset(offset).count
								result["works"] = ProjectApi.build_work(work_users)
								result["count"] = work_count.length
								result["previous_page"] = (params[:page].to_i - 1).to_s
								if (work_count.length) > (params[:page].to_i * per_page)
									result["next_page"] = params[:page].to_i + 1
								else
									result["next_page"] = ""
								end
								result
							end
						end
					end
				end
			end
		end

		# 工作申请详情接口
		# 接口编号:2.6.7
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## work_id    type:String  工作ID
		# 请求方式: GET
		# 返回: 工作申请详情
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd,    type: String, desc: '密码'
			optional :work_id,   type: String, desc: '工作id'
		end
		get 'workapl_detail' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:work_id].nil?
				{code: "400", msg: "缺少项目ID"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					work = Work.where(id: params[:work_id], work_type: 1).first
					if work.nil?
						{code: "400", msg: "查询不到该工作"}
					else
						if work.verified_type == 1 || work.verified_type == 3
							if work.publish_user == user_id || work.verified_user == user_id ||  UserAPI.auth_user(user_id) == -99
								user_list = []
								works_users = WorkUser.where(work_id: work.id, user_type: 3)
								works_users.each do |wu|
									work_user = Hash.new
									work_user["user_id"] = wu.user_id
									if wu.confirm_status == 1
										work_user["confirm_status"] = "未确认"
									elsif wu.confirm_status == 2
										work_user["confirm_status"] = "未确认"
									else
										work_user["confirm_status"] = "已确认"
									end
									user_list << work_user
								end
								if work.verified_type == 1
									verified_type = "未审核"
								elsif work.verified_type == 2
									verified_type = "审核通过"
								else
									verified_type = "审核未通过"
								end
									
								{
									code: "0",
									msg: "OK",
									work: {
										work: work.content,
										publish_time: work.created_at.strftime("%Y-%m-%d %H:%M"),
										verified_user: work.verified_user,
										verified_type: verified_type,
										user_list: user_list
									}
								}
							else
								{code: "400", msg: "不是工作人员"}
							end
						elsif work.verified_type == 2
							user_list = []
							works_users = WorkUser.where(work_id: work.id, user_type: 3)
							works_users.each do |wu|
								work_user = Hash.new
								work_user["user_id"] = wu.user_id
								if wu.confirm_status == 1
									work_user["confirm_status"] = "未确认"
								elsif wu.confirm_status == 2
									work_user["confirm_status"] = "未确认"
								else
									work_user["confirm_status"] = "已确认"
								end
								user_list << work_user
							end
							if work.verified_type == 1
								verified_type = "未审核"
							elsif work.verified_type == 2
								verified_type = "审核通过"
							else
								verified_type = "审核未通过"
							end								
							{
								code: "0",
								msg: "OK",
								work: {
									work: work.content,
									publish_time: work.created_at.strftime("%Y-%m-%d %H:%M"),
									verified_user: work.verified_user,
									verified_type: verified_type,
									user_list: user_list
								}
							}
						end
					end
				end
			end
		end

		# 工作安排详情接口
		# 接口编号:2.6.8
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## work_id    type:String  工作ID
		# 请求方式: GET
		# 返回: 工作申请详情
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd,    type: String, desc: '密码'
			optional :work_id,   type: String, desc: '工作id'
		end
		get 'workarg_detail' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:work_id].nil?
				{code: "400", msg: "缺少工作ID"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					work = Work.where(id: params[:work_id], work_type: 2).first
					if work.nil?
						{code: "400", msg: "查询不到该工作"}
					else
							user_list = []
							works_users = WorkUser.where(work_id: work.id, user_type: 3)
							works_users.each do |wu|
								work_user = Hash.new
								work_user["user_id"] = wu.user_id
								if wu.confirm_status == 1
									work_user["confirm_status"] = "未确认"
								elsif wu.confirm_status == 2
									work_user["confirm_status"] = "未确认"
								else
									work_user["confirm_status"] = "已确认"
								end
								user_list << work_user
							end
							{
								code: "0",
								msg: "OK",
								work: {
									work: work.content,
									publish_time: work.created_at.strftime("%Y-%m-%d %H:%M"),
									user_list: user_list
								}
							}
					end
				end
			end
		end

		# 工作审核接口
		# 接口编号:2.6.9
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## work_id    type:String  工作ID
		## verified_type type:Integer 审核状态，2通过，3不通过
		# 请求方式: GET
		# 返回: 工作申请详情
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd,    type: String, desc: '密码'
			optional :work_id,   type: String, desc: '工作id'
			optional :verified_type, type: Integer, desc: '审核状态'
		end
		post 'workapl_verified' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:work_id].nil?
				{code: "400", msg: "缺少工作ID"}
			elsif params[:verified_type].nil?
				{code: "400", msg: "缺少审核状态"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					work = Work.where(id: params[:work_id], verified_user: user_id, work_type: 1).first
					if work.nil?
						{code: "400", msg: "工作不存在或者没有审核权限"}
					else
						if work.verified_type == 2
							{code: "400", msg: "工作已经审核通过，不要重复审核"}
						elsif work.verified_type == 3
							{code: "400", msg: "工作已经审核不通过，不要重复审核"}
						else
							if params[:verified_type] == 2
								work.verified_type = 2
								work.updated_at = Time.now
								project = Project.find(work.project_id)
								publish_user_info = User.find(work.publish_user)
								if work.save
									MessageApi.new_message2(work.publish_user, 2, 4, work.id, work.project_id, "工作申请审核反馈","您在"+project.name+"项目发布的工作申请已被通过", "", publish_user_info.name)
									WorkUser.where(work_id: work.id).update_all(confirm_status: 2, updated_at: Time.now)
									works_users = WorkUser.where("work_id = ? AND user_type = 3 AND (event_id IS NULL OR event_id = -1)", work.id)
									send_user = User.find(work.publish_user)
									project = Project.find(work.project_id)
									works_users.each do |wu|
										wu.event_id = EventApi.make_event(wu.user_id, wu.project_id, 1, wu.work_id)
										wu.save
										MessageApi.new_message2(wu.user_id, 2, 4, wu.work_id, wu.project_id, "工作安排", send_user.name+"在"+project.name+"项目给您发布了一条工作安排。", "", publish_user_info.name)
									end
									event_id = WorkUser.where(work_id: work.id, user_type: 1).first.event_id
									if !(event_id.nil?)
										EventApi.process_event(event_id)
									end
									######+++++这里需要加逻辑代办消息取消，加个代办消息
									{code: "0", msg: "OK"}
								else
									{code: "400", msg: "审核失败"}
								end
							elsif params[:verified_type] == 3
								work.verified_type = 2
								work.updated_at = Time.now
								verified_user_info = User.find(user_id)
								if work.save
									MessageApi.new_message2(work.publish_user, 2, 4, work.id, work.project_id, "工作申请审核反馈", "您在"+project.name+"项目发布的工作申请已被拒绝", "", verified_user_info.name)
									# WorkUser.where(work_id: work.id).update_all(confirm_status: 3, updated_at: Time.now)
									# works_users = WorkUser.where("work_id = ? AND user_type = 3 AND (event_id IS NULL OR event_id = -1)", work.id)
									# works_users.each do |wu|
									# 	wu.event_id = EventApi.make_event(wu.user_id, wu.project_id, 1, wu.work_id)
									# 	wu.save
									# 	MessageApi.new_message(wu.user_id, 2, 4, wu.work_id, wu.project_id, "您有一条工作，请确认")
									# end
									event_id = WorkUser.where(work_id: work.id, user_type: 1).first.event_id
									if !(event_id.nil?)
										EventApi.process_event(event_id)
									end
									######+++++这里需要加逻辑代办消息取消，加个代办消息
									{code: "0", msg: "OK"}
								else
									{code: "400", msg: "审核失败"}
								end 
							else
								{code: "400", msg: "审批状态不正确"}
							end
								
							
						end
					end
				end
			end
		end

		# 工作审核转派接口（OK）
		# 接口编号:2.6.10
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## work_id    type:String  工作ID
		## verified_user type:String 转派人
		# 请求方式: Post
		# 返回: 工作申请详情
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd,    type: String, desc: '密码'
			optional :work_id,   type: String, desc: '工作id'
			optional :verified_user, type: String, desc: '转派人'
		end
		post 'workverified_send' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:work_id].nil?
				{code: "400", msg: "缺少工作ID"}
			elsif params[:verified_user].nil?
				{code: "400", msg: "缺少转派人"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					work = Work.where(id: params[:work_id], verified_user: user_id, work_type: 1).first
					if work.nil?
						{code: "400", msg: "工作不存在或者没有审核权限"}
					else
						if work.verified_type == 2
							{code: "400", msg: "工作已经审核通过，不可再转派"}
						elsif work.verified_type == 3
							{code: "400", msg: "工作已经审核不通过，不可再转派"}
						else
							project_user = ProjectUser.where(project_id: work.project_id, user_id: params[:verified_user]).first
							if project_user.nil?
								{code: "400", msg: "非项目组成员不可转派"}
							else
								work.verified_user = params[:verified_user]
								work.updated_at = Time.now
								if work.save
									work_user = WorkUser.where(work_id: work.id, user_type: 1).first
									work_user.user_id = params[:verified_user]
									work_user.updated_at = Time.now
									if !(work_user.event_id.nil?)
										EventApi.process_event(work_user.event_id)
									end
									work_user.event_id = EventApi.make_event(work_user.user_id, work_user.project_id, 1, work_user.work_id)
									if work_user.save
										user = User.find(user_id)
										project = Project.find(work.project_id)
										MessageApi.new_message2(params[:verified_user], 2, 4, work.id, work.project_id, "工作申请审核转发", user.name+"在"+project.name+"项目给您转发了一条工作申请，请审核。", "", user.name)
										{code: "0", msg: "OK"}
									else
										{code: "400", msg: "转派失败"}
									end	
								else
									{code: "400", msg: "转派失败"}
								end
							end
						end
					end
				end
			end
		end

		# 工作转发接口
		# 接口编号:2.6.11
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## work_id    type:String  工作ID
		## user_list  type:String  转发人
		# 请求方式: Post
		# 返回: 工作申请详情
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd,    type: String, desc: '密码'
			optional :work_id,   type: String, desc: '工作id'
			optional :user_list, type: String, desc: '转发人'
		end
		post 'work_send' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:work_id].nil?
				{code: "400", msg: "缺少工作ID"}
			elsif params[:user_list].nil?
				{code: "400", msg: "缺少转派人"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					work = Work.where(id: params[:work_id]).first
					send_user = User.find(user_id)
					project = Project.find(work.project_id)
					if work.nil?
						{code: "400", msg: "工作不存"}
					else
						if work.verified_type == 1
							(params[:user_list].split(",")).each do |ul|
								ProjectApi.create_work_user(ul.to_i, work.project_id, work.id, work.work_type, 3, 1)
							end
							{code: "0", msg: "OK"}
						elsif work.verified_type == 2									
							((params[:user_list])[1..-2].split(",")).each do |ul|
								ProjectApi.create_work_user(ul.to_i, work.project_id, work.id, work.work_type, 3, 2)
								module_type = 3
								if work.work_type == 1
									module_type = 4
								end
								MessageApi.new_message2(ul.to_i, 2, module_type, work.id, work.project_id, "工作安排转发", send_user.name+"在"+project.name+"项目转发给您一条工作安排。", "", send_user.name)
							end
							{code: "0", msg: "OK"}
						else
							{code: "400", msg: "未审核通过，不允许转派"}
						end
					end
				end
			end
		end

		# 工作确认接口
		# 接口编号:2.6.12
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## work_id    type:String  工作ID
		# 请求方式: Post
		# 返回: 
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd,    type: String, desc: '密码'
			optional :work_id,   type: String, desc: '工作id'
		end
		post 'work_confirm' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:work_id].nil?
				{code: "400", msg: "缺少工作ID"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					work = Work.where(id: params[:work_id]).first
					if work.nil?
						{code: "400", msg: "工作不存在"}
					else
						work_user = WorkUser.where(work_id: params[:work_id], user_id: user_id, confirm_status: 2, user_type: 3).first
						if work_user.nil?
							{code: "400", msg: "不是工作相关人员，工作不可进行或者已经确认"}
						else
							user = User.find(user_id)
							project = Project.find(work.project_id)
							work_user.confirm_status = 3
							if work_user.save
								if !(work_user.event_id.nil?)
									EventApi.process_event(work_user.event_id)
								end
								if work.work_type == 1
									# new_message 方法用于工作申请和安排，相关人员确认并发送通知
									### user_id
									### message_type //消息类别  1.系统消息 2.项目消息
									### module_type  //消息类型  1.加入项目, 2.离开项目, 3.工作安排, 4.工作申请, 5.工作评价, 6.项目公告
									### module_id    //对应的类型id,如果module_type是1或者2，则module_id是project_id,如果module_type是3或者4，则module_id是工作ID
									### content		   //消息内容
									#####工作申请确认
									##  确认完要给发布人通知功能去掉
									## MessageApi.new_message(work.publish_user, 2, 4, work.id, work.project_id, "工作申请确认", user.name+"确认了您在"+project.name+"项目发布的工作安排。")
								else
									#####工作安排确认
									##  确认完要给发布人通知功能去掉
									## MessageApi.new_message(work.publish_user, 2, 3, work.id, work.project_id, "工作安排确认", user.name+"确认了您在"+project.name+"项目发布的工作安排。")
								end								
								{code: "0", msg: "OK"}
							else
								{code: "400", msg: "确认失败"}
							end
						end
					end
				end
			end
		end

		########><><><><><><><><><><><>项目评价相关接口<><><><><><><><><><>#############

		# 我的评价接口
		# 接口编号:2.7.1
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## project_id type:String  项目ID
		## page       type:Integer 页码
		# 请求方式: post
		# 返回: 工作ID
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd, type: String, desc: '密码'
			optional :project_id, type: String, desc: '项目ID'
			optional :page, type: Integer, desc: '工作内容'
		end
		get 'my_evaluations' do 
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					per_page = 10
					project = Project.where(id: params[:project_id], publish_status: 1).first
					project_user = ProjectUser.where(project_id: params[:project_id], user_id: user_id).first
					if project.nil? || project_user.nil? 
						{code: "400", msg: "项目不存在或你不是项目成员"}
					else
						result = Hash.new
						result["code"] = "0"
						result["msg"] = "OK"
						if params[:page].nil? || params[:page] == 1
							evaluations_users = EvaluationUser.where("((user_id = ? and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("evaluation_id").order(updated_at: :desc).limit(per_page)
							evaluations_count = EvaluationUser.where("((user_id = ? and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("evaluation_id").order(updated_at: :desc).limit(per_page).count
							result["evaluations"] = ProjectApi.build_evaluation(evaluations_users)
							result["count"] = evaluations_count.length
							result["previous_page"] = ""
							if (evaluations_count.length) > per_page
								result["next_page"] = "2"
							else
								result["next_page"] = ""
							end
							result
						else
							offset = (params[:page].to_i - 1) * per_page
							evaluations_users = EvaluationUser.where("((user_id = ? and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("evaluation_id").order(updated_at: :desc).limit(per_page).offset(offset)
							evaluations_count = EvaluationUser.where("((user_id = ? and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("evaluation_id").order(updated_at: :desc).limit(per_page).offset(offset).count
							result["evaluations"] = ProjectApi.build_evaluation(evaluations_users)
							result["count"] = evaluations_count.length
							result["previous_page"] = (params[:page].to_i - 1).to_s
							if (evaluations_count.length) > (params[:page].to_i * per_page)
								result["next_page"] = params[:page].to_i + 1
							else
								result["next_page"] = ""
							end
							result
						end
					end
				end
			end
		end

		# 全部评价接口
		# 接口编号:2.7.2
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## project_id    type:String  工作ID
		## page       type:Integer  页码
		# 请求方式: get
		# 返回: 工作ID
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd, type: String, desc: '密码'
			optional :project_id, type: String, desc: '项目ID'
			optional :page, type: Integer, desc: '工作内容'
		end
		get 'all_evaluations' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					per_page = 10
					project = Project.where(id: params[:project_id], publish_status: 1).first
					project_user = ProjectUser.where(project_id: params[:project_id], user_id: user_id).first
					if project.nil? || (project_user.nil? && !(UserAPI.auth_user(user_id) == -99))
						{code: "400", msg: "项目不存在或你不是项目成员"}
					else
						result = Hash.new
						result["code"] = "0"
						result["msg"] = "OK"
						if params[:page].nil? || params[:page] == 1
							if UserAPI.auth_user(user_id) == -99
								evaluations_users = Evaluation.where("project_id = ? ", params[:project_id]).order(updated_at: :desc).limit(per_page)
								evaluations_count = Evaluation.where("project_id = ? ", params[:project_id]).order(updated_at: :desc).count
								result["evaluations"] =ProjectApi.build_admin_evaluation(evaluations_users)
								result["count"] = evaluations_count
								result["previous_page"] = ""
								if (evaluations_count) > per_page
									result["next_page"] = "2"
								else
									result["next_page"] = ""
								end
								result
							else
								evaluations_users = EvaluationUser.where("((user_id = ?  and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 ) or (confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("evaluation_id").order(updated_at: :desc).limit(per_page)
								evaluations_count = EvaluationUser.where("((user_id = ?  and user_type = 1) or (user_id = ? and user_type = 2 ) or (user_id = ? and user_type = 3 and confirm_status != 1 ) or (confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("evaluation_id").limit(per_page).count
								result["evaluations"] =ProjectApi.build_evaluation(evaluations_users)
								result["count"] = evaluations_count.length
								result["previous_page"] = ""
								if (evaluations_count.length) > per_page
									result["next_page"] = "2"
								else
									result["next_page"] = ""
								end
								result
							end
						else
							if UserAPI.auth_user(user_id) == -99
								offset = (params[:page].to_i - 1) * per_page
								evaluations_users = Evaluation.where("project_id = ? ", params[:project_id]).order(updated_at: :desc).limit(per_page).offset(offset)
								evaluations_count = Evaluation.where("project_id = ? ", params[:project_id]).order(updated_at: :desc).offset(offset).count
								result["evaluations"] = ProjectApi.build_admin_evaluation(evaluations_users)
								result["count"] = evaluations_count
								result["previous_page"] = (params[:page].to_i - 1).to_s
								if (evaluations_count) > (params[:page].to_i * per_page)
									result["next_page"] = params[:page].to_i + 1
								else
									result["next_page"] = ""
								end
								result
							else
								offset = (params[:page].to_i - 1) * per_page
								evaluations_users = EvaluationUser.where("((user_id = ?  and user_type = 1) 
							 	or (user_id = ? and user_type = 2 )
							 	or (user_id = ? and user_type = 3 and confirm_status != 1 )
							 	or (confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("evaluation_id").order(updated_at: :desc).limit(per_page).offset(offset)
								evaluations_count = EvaluationUser.where("((user_id = ?  and user_type = 1) 
							 	or (user_id = ? and user_type = 2 )
							 	or (user_id = ? and user_type = 3 and confirm_status != 1 )
							 	or (confirm_status != 1 )) AND project_id = ? ", user_id, user_id, user_id, params[:project_id]).group("evaluation_id").order(updated_at: :desc).limit(per_page).offset(offset).count
								result["evaluations"] = ProjectApi.build_evaluation(evaluations_users)
								result["count"] = evaluations_count.length
								result["previous_page"] = (params[:page].to_i - 1).to_s
								if (evaluations_count.length) > (params[:page].to_i * per_page)
									result["next_page"] = params[:page].to_i + 1
								else
									result["next_page"] = ""
								end
								result
							end
						end
					end
				end
			end
		end

		# 新建评价接口(ok-消息放到建立关系的时候创建)
		# 接口编号:2.7.3
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## project_id type:String  项目ID
		## score			type:Float   得分
		## attachment_01       type:File  工作内容
		## attachment_02       type:File  工作内容
		## attachment_03       type:File  工作内容
		## verified_user type:String 审核人
		## user_list  type:Integer 相关人员列表
		# 请求方式: post
		# 返回: 工作ID
		params do
			optional :user_name,     type: String, desc: '用户名'
			optional :passwd, 			 type: String, desc: '密码'
			optional :project_id, 	 type: String, desc: '项目ID'
			optional :score, 	       type: Float,  desc: '得分'
			optional :attachment_01, type: File,   desc: '评价附件01'
			optional :attachment_02, type: File,   desc: '评价附件02'
			optional :attachment_03, type: File,   desc: '评价附件03'
			optional :verified_user, type: String, desc: '审核人'
			optional :user_list, type: String,     desc: '相关人员列表'
		end
		post 'evaluate' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:user_list].nil?
				{code: "400", msg: "缺少项目相关人员"}
			elsif params[:project_id].nil?
				{code: "400", msg: "缺少项目ID"}
			elsif params[:verified_user].nil?
				{code: "400", msg: "缺少审核人"}
			elsif params[:score].nil?
				{code: "400", msg: "缺少得分"}
			elsif params[:attachment_01].nil? && params[:attachment_02].nil? && params[:attachment_03].nil?  
				{code: "400", msg: "至少提交一个附件"}	
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名密码错误"}
				else
					project_user = ProjectUser.where(project_id: params[:project_id], user_id: user_id).first
					project = Project.where(id: params[:project_id], publish_status: 1).first
					if project_user.nil? || project_user.role == 2
						{code: "400", msg: "不是项目内部人员"}
					elsif project.nil?
						{code: "400", msg: "项目已禁用"}
					else

						evaluation = Evaluation.new
						file01 = params[:attachment_01]
						file02 = params[:attachment_02]
						file03 = params[:attachment_03]
						if !(file01.nil?)
							url01 = CommonApi::random_string + file01['filename']
							code01 = CommonApi::url_post(file01, url01)
							if code01 == 200
								url01 = CommonApi::URL_QN + "/" + url01
								evaluation.attachment_01 = url01
							end
						end
						if !(file02.nil?)
							url02 = CommonApi::random_string + file02['filename']
							code02 = CommonApi::url_post(file02, url02)
							if code02 == 200
								url02 = CommonApi::URL_QN + "/" + url02
								evaluation.attachment_02 = url02
							end
						end
						if !(file03.nil?)
							url03 = CommonApi::random_string + file03['filename']
							code03 = CommonApi::url_post(file03, url03)
							if code03 == 200
								url03 = CommonApi::URL_QN + "/" + url03
								evaluation.attachment_03 = url03
							end
						end
						evaluation.project_id = params[:project_id]
						evaluation.score = params[:score]
						evaluation.publish_user = user_id
						evaluation.verified_type = 1
						evaluation.verified_user = params[:verified_user]
						evaluation.created_at = Time.now
						evaluation.updated_at = Time.now
						if evaluation.save
							ProjectApi.create_evaluation_user(user_id, params[:project_id], evaluation.id, 2, 1)   # 新增一条发布人关系
							ProjectApi.create_evaluation_user(params[:verified_user], params[:project_id], evaluation.id, 1, 1)		# 新增一条审核人关系
							((params[:user_list])[1..-2].split(",")).each do |ul|
								ProjectApi.create_evaluation_user(ul.to_i, params[:project_id], evaluation.id, 3, 1)
							end
							user = User.find(user_id)
							project = Project.find(params[:project_id])
							MessageApi.new_message2(params[:verified_user], 2, 5, evaluation.id, params[:project_id], "工作评价审核", user.name+"在"+project.name+"项目发布一条工作评价，请审核。", "", user.name)
							{code: "0", msg: "OK"}
						else
							{code: "400", msg: "保存失败"}
						end
					end
				end
			end
		end

		# 评价审核接口(ok)
		# 接口编号:2.7.4
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## evaluation_id    type:String  工作ID
		## verified_type type:Integer 审核状态，2通过，3不通过
		# 请求方式: POST
		# 返回: 是否成功
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd,    type: String, desc: '密码'
			optional :evaluation_id,   type: String, desc: '工作id'
			optional :verified_type, type: Integer, desc: '审核状态'
		end
		post 'verified_evaluation' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:evaluation_id].nil?
				{code: "400", msg: "缺少工作ID"}
			elsif params[:verified_type].nil?
				{code: "400", msg: "缺少审核状态"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					verified_user_info = User.find(user_id)
					evaluation = Evaluation.where(id: params[:evaluation_id], verified_user: user_id).first
					if evaluation.nil?
						{code: "400", msg: "评价不存在或者没有审核权限"}
					else
						if evaluation.verified_type == 2
							{code: "400", msg: "评价已经审核通过，不要重复审核"}
						elsif evaluation.verified_type == 3
							{code: "400", msg: "评价已经审核不通过，不要重复审核"}
						else
							project = Project.find(evaluation.project_id)
							user = User.find(evaluation.publish_user)
							if params[:verified_type] == 2
								evaluation.verified_type = 2
								evaluation.updated_at = Time.now
								if evaluation.save
									evaluation_user = EvaluationUser.where(evaluation_id: evaluation.id)
									evaluation_user.each do |eu|
										eu.confirm_status = 2
										eu.updated_at =Time.now
										eu.save
										if eu.user_type == 2
											MessageApi.new_message2(eu.user_id, 2, 5, eu.evaluation_id, eu.project_id, "工作评价审核反馈", "您在"+project.name+"项目发布的工作评价已被通过。", "", verified_user_info.name)
										elsif eu.user_type ==3
											MessageApi.new_message2(eu.user_id, 2, 5, eu.evaluation_id, eu.project_id, "工作评价", user.name+"在"+project.name+"项目给您的工作做了一次工作评价。", "", user.name)
										end										
										if eu.user_type == 1
											EventApi.process_event(eu.event_id)
										end
									end
									{code: "0", msg: "OK"}
								else
									{code: "400", msg: "审核失败"}
								end
							elsif params[:verified_type] == 3							
								evaluation.verified_type = 3
								evaluation.updated_at = Time.now
								if evaluation.save
									evaluation_user = EvaluationUser.where(evaluation_id: evaluation.id)
									evaluation_user.each do |eu|
										# eu.confirm_status = 2
										# eu.updated_at =Time.now
										# eu.save
										if eu.user_type == 2
											MessageApi.new_message2(eu.user_id, 2, 5, eu.evaluation_id, eu.project_id, "工作评价审核反馈", "您在"+project.name+"项目发布的工作评价已被拒绝。", "", verified_user_info)
										end										
										if eu.user_type == 1
											EventApi.process_event(eu.event_id)
										end
									end
									{code: "0", msg: "OK"}
								else
									{code: "400", msg: "审核失败"}
								end
							else
								{code: "400", msg: "审核参数不正确"}
							end
						end
					end
				end
			end
		end

		# 评价审核转派接口(OK)
		# 接口编号:2.7.5
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## evaluation_id    type:String  工作ID
		## verified_user type:String 转派人
		# 请求方式: Post
		# 返回: 工作申请详情
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd,    type: String, desc: '密码'
			optional :evaluation_id,   type: String, desc: '评价id'
			optional :verified_user, type: String, desc: '转派人'
		end
		post 'evaluationverified_send' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:evaluation_id].nil?
				{code: "400", msg: "缺少评价ID"}
			elsif params[:verified_user].nil?
				{code: "400", msg: "缺少转派人"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					evaluation = Evaluation.where(id: params[:evaluation_id], verified_user: user_id).first
					if evaluation.nil?
						{code: "400", msg: "评价不存在或者没有转派权限"}
					else
						if evaluation.verified_type == 2
							{code: "400", msg: "评价已经审核通过，不可再转派"}
						elsif evaluation.verified_type == 3
							{code: "400", msg: "评价已经审核不通过，不可再转派"}
						else
							project_user = ProjectUser.where(project_id: evaluation.project_id, user_id: params[:verified_user]).first
							if project_user.nil?
								{code: "400", msg: "非项目组成员不可转派"}
							else
								evaluation.verified_user = params[:verified_user]
								evaluation.updated_at = Time.now
								if evaluation.save
									evaluation_user = EvaluationUser.where(evaluation_id: evaluation.id, user_type: 1).first
									EventApi.process_event(evaluation_user.event_id)
									evaluation_user.event_id = EventApi.make_event(params[:verified_user], evaluation_user.project_id, 3, evaluation_user.evaluation_id)
									evaluation_user.user_id = params[:verified_user]
									evaluation_user.updated_at = Time.now
									if evaluation_user.save
										send_user = User.find(user_id)
										project = Project.find(evaluation.project_id)
										MessageApi.new_message2(params[:verified_user], 2, 5, evaluation_user.evaluation_id, evaluation_user.project_id, "工作评价审核转发", send_user.name+"在"+project.name+"项目给您转发了一条工作评价，请审核。", "", send_user.name)
										{code: "0", msg: "OK"}
									else
										{code: "400", msg: "转派失败"}
									end	
								else
									{code: "400", msg: "转派失败"}
								end
							end
						end
					end
				end
			end
		end

		# 评价详情接口
		# 接口编号:2.7.6
		# 参数:
		## user_name  type:String  用户名
		## passwd     type:String  密码
		## evaluation_id    type:String  评价ID
		# 请求方式: GET
		# 返回: 工作申请详情
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd,    type: String, desc: '密码'
			optional :evaluation_id,   type: String, desc: '工作id'
		end
		get 'evaluation_detail' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			elsif params[:evaluation_id].nil?
				{code: "400", msg: "缺少项目ID"}
			else
				user_id = UserAPI.user_login_new(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					evaluation = Evaluation.where(id: params[:evaluation_id]).first
					if evaluation.nil?
						{code: "400", msg: "查询不到该工作"}
					else
						if evaluation.verified_type == 1 || evaluation.verified_type == 3
							if evaluation.publish_user == user_id || evaluation.verified_user == user_id
								user_list = []
								evaluations_users = EvaluationUser.where(evaluation_id: evaluation.id, user_type: 3)
								evaluations_users.each do |eu|
									user_list << eu.user_id
								end									
								{
									code: "0",
									msg: "OK",
									evaluation: {
										id: evaluation.id,
										score: evaluation.score.nil?? 0 : evaluation.score,
										verified_type: evaluation.verified_type,
										verified_user: evaluation.verified_user,
										publish_user: evaluation.publish_user,
										attachment_01: evaluation.attachment_01.nil?? "" : evaluation.attachment_01,
										attachment_02: evaluation.attachment_02.nil?? "" : evaluation.attachment_02,
										attachment_03: evaluation.attachment_03.nil?? "" : evaluation.attachment_03,
										publish_time: evaluation.created_at.strftime("%Y-%m-%d %H:%M"),
										user_list: user_list
									}
								}
							else
								{code: "400", msg: "不是工作人员"}
							end
						elsif evaluation.verified_type == 2
							user_list = []
							evaluations_users = EvaluationUser.where(evaluation_id: evaluation.id, user_type: 3)
							evaluations_users.each do |eu|
								user_list << eu.user_id
							end							
							{
								code: "0",
								msg: "OK",
								evaluation: {
									id: evaluation.id,
									score: evaluation.score.nil?? 0 : evaluation.score,
									verified_type: evaluation.verified_type,
									verified_user: evaluation.verified_user,
									publish_user: evaluation.publish_user,
									attachment_01: evaluation.attachment_01.nil?? "" : evaluation.attachment_01,
									attachment_02: evaluation.attachment_02.nil?? "" : evaluation.attachment_02,
									attachment_03: evaluation.attachment_03.nil?? "" : evaluation.attachment_03,
									publish_time: evaluation.created_at.strftime("%Y-%m-%d %H:%M"),
									user_list: user_list
								}
							}
						end
					end
				end
			end
		end

		desc '查询已认证项目列表'
		params do
			optional :company_id, type: String, desc: '公司ID'
			optional :page,       type: Integer, desc: '页码'
		end
		get 'certification_list' do
			content_type 'application/x-www-form-urlencoded'
			per_page = 2
			if params[:company_id].nil?
				{code: '400', msg: '参数不足'}
			else
				if params[:page].nil? || params[:page] == 1
					projects = Project.where(certification_company: params[:company_id], certification_status: 1).order(create_at: :desc).limit(per_page)
					page_count = Project.where(certification_company: params[:company_id], certification_status: 1).order(create_at: :desc).count
					project_list = []
					projects.each do |pj|
						project = Hash.new
						project["id"] = pj.id
						project["name"] = pj.name
						project["desc"] = pj.project_desc
						project["created_at"] = pj.create_at.to_time.strftime('%Y-%m-%d %H:%M:%S')
						project["certification_status"] = pj.certification_status
						project_users = ProjectUser.where(project_id: pj.id)
						project["user_num"] = project_users.count
						works = Work.where(project_id: pj.id)
						project["work_num"] = works.count
						project_list << project
					end
					if page_count > per_page
						next_page = "2"
					else
						next_page = ""
					end
					{
						code: "0",
						msg: "OK",
						project_list: project_list,
						page_count: page_count,
						previous_page: "",
						next_page: next_page
					}
				else
					offset = (params[:page].to_i - 1) * per_page
					projects = Project.where(certification_company: params[:company_id], certification_status: 1).order(create_at: :desc).limit(per_page).offset(offset)
					page_count = Project.where(certification_company: params[:company_id], certification_status: 1).order(create_at: :desc).count
					project_list = []
					projects.each do |pj|
						project = Hash.new
						project["id"] = pj.id
						project["name"] = pj.name
						project["desc"] = pj.project_desc
						project["created_at"] = pj.create_at.to_time.strftime('%Y-%m-%d %H:%M:%S')
						project["certification_status"] = pj.certification_status
						project_users = ProjectUser.where(project_id: pj.id)
						project["user_num"] = project_users.count
						works = Work.where(project_id: pj.id)
						project["work_num"] = works.count
						project_list << project
					end
					if page_count > (params[:page].to_i * per_page)
						next_page = params[:page].to_i + 1
					else
						next_page = ""
					end
					{
						code: "0",
						msg: "OK",
						project_list: project_list,
						page_count: projects.count,
						previous_page: (params[:page].to_i - 1).to_s,
						next_page: next_page

					}
				end
			end
		end
		
		desc '查询项目列表申请'
		params do
			optional :company_id, type: String, desc: '公司ID'
			optional :page,       type: Integer, desc: '页码'
		end
		get 'apply_list' do
			content_type 'application/x-www-form-urlencoded'
			per_page = 2
			if params[:company_id].nil?
				{code: '400', msg: '参数不足'}
			else
				if params[:page].nil? || params[:page] == 1
					projects = Project.where(certification_company: params[:company_id], certification_status: 0).order(create_at: :desc).limit(per_page)
					page_count = Project.where(certification_company: params[:company_id], certification_status: 0).order(create_at: :desc).count
					project_list = []
					projects.each do |pj|
						project = Hash.new
						project["id"] = pj.id
						project["name"] = pj.name
						project["desc"] = pj.project_desc
						project["created_at"] = pj.create_at.to_time.strftime('%Y-%m-%d %H:%M:%S')
						project["certification_status"] = pj.certification_status
						project_users = ProjectUser.where(project_id: pj.id)
						project["user_num"] = project_users.count
						works = Work.where(project_id: pj.id)
						project["work_num"] = works.count
						project["apply_reason"] = pj.apply_reason
						project_list << project
					end
					if page_count > per_page
						next_page = "2"
					else
						next_page = ""
					end
					{
						code: "0",
						msg: "OK",
						project_list: project_list,
						page_count: page_count,
						previous_page: "",
						next_page: next_page
					}
				else
					offset = (params[:page].to_i - 1) * per_page
					projects = Project.where(certification_company: params[:company_id], certification_status: 0).order(create_at: :desc).limit(per_page).offset(offset)
					page_count = Project.where(certification_company: params[:company_id], certification_status: 0).order(create_at: :desc).count
					project_list = []
					projects.each do |pj|
						project = Hash.new
						project["id"] = pj.id
						project["name"] = pj.name
						project["desc"] = pj.project_desc
						project["created_at"] = pj.create_at.to_time.strftime('%Y-%m-%d')
						project["certification_status"] = pj.certification_status
						project_users = ProjectUser.where(project_id: pj.id)
						project["user_num"] = project_users.count
						works = Work.where(project_id: pj.id)
						project["work_num"] = works.count
						project["apply_reason"] = pj.apply_reason
						project_list << project
					end
					if page_count > (params[:page].to_i * per_page)
						next_page = params[:page].to_i + 1
					else
						next_page = ""
					end
					{
						code: "0",
						msg: "OK",
						project_list: project_list,
						page_count: projects.count,
						previous_page: (params[:page].to_i - 1).to_s,
						next_page: next_page

					}
				end
			end
		end

		desc '查询项目申请详情'
		params do
			optional :project_id, type: String, desc: '项目申请'
		end
		get 'get_apply_project' do
			content_type 'application/x-www-form-urlencoded'
			if params[:project_id].nil?
				{code: '400', msg: '缺少参数'}
			else
				project = Project.find(params[:project_id])
				if project.nil?
					{code: '400', msg: '未查询到该项目'}
				else
					project_user = ProjectUser.where(project_id: params[:project_id], role: 1).first

					user = User.find(project_user.user_id)
					if project.certification_company.nil?
						company_name = ""
					else
						company = Company.find(project.certification_company)
						if company.nil?
							company_name = ""
						else
							company_name = company.company_name
						end
					end
					
					{
						code: '0',
						msg: 'OK',
						apply_info:{
							'company_name': company_name,
							'project_id': project.id,
							'project_name': project.name,
							'project_manager': user.name.nil? ? "" : user.name,
							'phone': user.phone.nil? ? "" : user.phone,
							"apply_reason": project.apply_reason,
							"certification_status": project.certification_status
						}
					}
				end	
			end
		end

		desc '处理项目申请'
		params do
			optional :project_id, type: String, desc: '项目申请'
			optional :certification_status, type: String, desc: '项目申请'
			optional :app_id, type: String, desc: '上传app_id'
			optional :secret_key, type: String, desc: '上传secret_key'
		end
		post 'certificate' do
			content_type 'application/x-www-form-urlencoded'
			if params[:project_id].nil? ||params[:certification_status].nil? 
				{code: '400', msg: '参数不合法'}
			else
				project = Project.find(params[:project_id])
				project.certification_status = params[:certification_status]
				project.app_id =  params[:app_id]
				project.secret_key = params[:secret_key]
				project.save
				company  = Company.find(project.certification_company)
				project_team = ProjectTeam.new
				project_team.team_name = company.company_name + "默认项目班组"
				project_team.company_id = company.id
				project_team.project_id =  project.id
				##project_team.team_code = rand(999999).to_s
				project_team.save
				{
					code: '0',
					msg: 'OK'
				}
			end
		end

		desc '申请加入企业'
		params do
			optional :project_id, type: String, desc: '项目申请'
			optional :company_id, type: String, desc: '企业ID'
			optional :apply_reason, type: String, desc: '申请原因'
		end
		post 'apply_certificate' do
			content_type 'application/x-www-form-urlencoded'
			if params[:project_id].nil? ||params[:company_id].nil? 
				{code: '400', msg: '参数不合法'}
			else
				project = Project.find(params[:project_id])
				project.certification_company = params[:company_id]
				project.apply_reason = params[:apply_reason]
				project.certification_status = 0
				project.save
				{
					code: '0',
					msg: 'OK',
					certification_status: '0'
				}
					
			end
		end

		desc '项目信息上传'
		params do
			optional :project_id, type: String, desc: '项目申请'
			optional :project_name, type: String, desc: ''
			optional :project_type, type: String, desc: ''
			optional :construction_name, type: String, desc: ''
			optional :construciton_code, type: String, desc: ''
			optional :address, type: String, desc: ''
			optional :begin_time, type: String, desc: ''
			optional :contact_name, type: String, desc: ''
			optional :contact_phone, type: String, desc: ''
			optional :project_status, type: String, desc: ''
			optional :bank_code,  type: String, desc: '工资代扣银行代码'
			optional :bank_name,  type: String, desc: '开户行名称'
			optional :bank_card,  type: String, desc: '银行卡号'
		end
		post 'information_upload' do
			content_type 'application/x-www-form-urlencoded'
			if params[:project_id].nil?
				{code: '400', msg: '参数不合法'}
			else
				puts params
				project = Project.find(params[:project_id])
				project.name = params[:project_name]
				project.project_type = params[:project_type]
				project.construction_name = params[:construction_name]
				project.construction_code = params[:construciton_code]
				project.project_address = params[:address]
				project.begin_time = params[:begin_time]
				project.contact_name = params[:contact_name]
				project.contact_phone = params[:contact_phone]
				project.project_status = params[:project_status]
				project.upload_status = 2
				project.bank_code = CommonApi.cover_bank_name_to_code(params[:bank_code])
				project.bank_name = params[:bank_name]
				project.bank_card = params[:bank_card]
				project.save
				# company = Company.find(project.certification_company)
				# builderLicenses = {'prjName': project.name, 'builderLicenseNum': project.construction_code}
				# data = {'contractorCorpCode': company.credit_code, 'contractorCorpName': company.name, 'name': project.name, 'category': project.project_type, 'builderLicenses': builderLicenses, 'areaCode': project.project_address, 'prjStatus': project.project_status}.to_json
				# aaa = WorkManageService.project_upload(data, project.id)
				# puts aaa
				{
					code: '0',
					msg: 'OK',
					upload_status: 2
				}
			end
		end

		desc '项目信息上传查询'
		params do
			optional :project_id, type: String, desc: '项目申请'
		end
		get 'information_query' do
			content_type 'application/x-www-form-urlencoded'
			if params[:project_id].nil? 
				{code: '400', msg: '参数不合法'}
			else
				project = Project.find(params[:project_id])
				if project.certification_company.nil? || project.certification_status =! 1
					company_name = ""
				else
					company = Company.find(project.certification_company)
					if company.nil?
						company_name = ""
					else
						company_name = company.company_name
					end
					
				end
				
				{
					code: '0',
					msg: 'OK',
					company_name: company_name,
					project_name: project.name.nil?? "" : project.name,
					project_type: project.project_type,
					construction_name: project.construction_name.nil?? "" : project.construction_name,
					construction_code: project.construction_code.nil?? "" : project.construction_code,
					address: CommonApi::cover_city(project.project_address),
					begin_time: project.begin_time.nil?? "" : project.begin_time,
					contact_name: project.contact_name.nil?? "" : project.contact_name,
					contact_phone: project.contact_phone.nil?? "" : project.contact_phone,
					project_status: project.project_status.nil?? 3 : project.project_status,
					upload_status: project.upload_status.nil?? 0 : project.upload_status,
					bank_code: project.bank_code.nil? ? "" : CommonApi.cover_bank_code_to_name(project.bank_code),
					bank_name:project.bank_name.nil?? "" : project.bank_name,
					bank_card:project.bank_card.nil?? "" : project.bank_card
				}					
			end
		end

		desc '项目人员上传'
		params do
			optional :project_id, type: String, desc: '项目申请'
			optional :user_list, type: String, desc: '项目申请'
			optional :begin_time, type: String, desc: "开始时间"
			optional :end_time, type: String, desc: "结束时间"
		end
		post 'project_user_upload' do
			content_type 'application/x-www-form-urlencoded'
			if params[:project_id].nil? ||params[:user_list].nil? 
				{code: '400', msg: '参数不合法'}
			else
				zhixing_status = 0 # 执行检查器，保障一个方法只有一个执行开始状态
				parent_id = 0 # 上级ID，执行如果需要异步使用
				user_list = params[:user_list].split(";")
				project = Project.find(params[:project_id])
				user_list.each do |ul|
					user_shangchuan = 0
					user_detail = UserDetail.where(user_id: ul).first
					project_user = ProjectUser.where(project_id: params[:project_id], user_id: ul).first
					user = User.find(ul)
					#######员工上传必须有一个默认班组，这里校验
					if project_user.upload_project_team.nil？
						project_team = ProjectTeam.where("company_id = ?". project.certification_company).first
						project_user.upload_project_team = project_team.id
						project_user.save
					end
					if project_user.nil?

					else
						signs = Sign.where("project_id = ? and create_at != update_at and user_id = ?", params[:project_id],ul)
						signs.each do |sg|							
							kqyibus = KQYiBu.where("kq_id= ? and upload_status in (1,2)", sg.id)
							if kqyibus.empty?
								kqyibu = KQYiBu.new
								kqyibu.kq_id  = sg.id
								kqyibu.project_id = params[:project_id]
								kqyibu.upload_status = 0
								kqyibu.save
								user_shangchuan = 1
							end
						end
						###缺少工资单##缺少的消息提醒
						payroll_details = PayrollDetail.where("project_user_id = ? ",project_user.id)
						payroll_details.each do |pd|
							if pd.paroll_code.nil?
								qr_log = QrLog.new
						      	qr_log.project_id = params[:project_id]
						      	qr_log.MSG = "您发起的#{user.name}员工工资上传失败，错误原因为：项目经理没有上传第三方工资单号"
						      	qr_log.save

							else
								gzyibus = GZYiBu.where("gz_id= ? and upload_status in (1,2)", pd.id)
								if gzyibus.empty?
									gzyibu = KQYiBu.new
									gzyibu.gz_id  = sg.id
									gzyibu.project_id = params[:project_id]
									gzyibu.upload_status = 0
									gzyibu.save
									user_shangchuan = 1
								end
							end
						end
						if user_shangchuan == 0
							ryyibus = RYYiBu.where("ry_id= ? and upload_status in (1,2)", project_user.id)
							if ryyibus.empty?
								ryyibu = RYYiBu.new
								ryyibu.ry_id  = project_user.id
								ryyibu.project_id = params[:project_id]
								ryyibu.upload_status = 0
								ryyibu.save
							end
						end
					end
					##这里漏了对缺卡的消息提醒
					
				end
				{code: '0', msg: '系统正在上传，请稍后查收上传结果'}
			end
		end

		desc '公示信息下载接口'
		params do
			optional :project_id, type: String, desc: '项目申请'
		end
		get 'publicity_download' do
			content_type 'application/x-www-form-urlencoded'
			#ProjectApi.create_upload_information(45)
			if params[:project_id].nil? 
				{code: '400', msg: '参数不合法'}

			else
				file_path = MakeExcel.project_excel(params[:project_id])				
				{
					code: '0',
					msg: 'OK',
					publicity_download_url: file_path
				}					
			end
		end
		desc '新建项目'
		params do
			optional :user_id, type: String, desc: '用户ID'
			optional :project_name, type: String, desc: '项目名'
			optional :project_desc, type: String, desc: '项目描述'
		end
		post 'create_project' do
			content_type 'application/x-www-form-urlencoded'
			#ProjectApi.create_upload_information(45)
			if params[:user_id].nil? || params[:project_name].nil?
				{code: '400', msg: '参数不合法'}
			else
				#新建项目管理员
				user_jl = User.find(params[:user_id])
				user = User.new
				user.name = rand(999999999).to_s
				passwd = rand(999999999)
				user.encrypted_password =  Digest::MD5.hexdigest("abcd1234")
				user.save
				user_detail = UserDetail.new
				user_detail.user_id = user.id
				user_detail.validate_type = 2
				user_detail.save
				CommonApi.send_yzm(user.name.force_encoding(Encoding::UTF_8), "abcd1234".force_encoding(Encoding::UTF_8), "http://app.hanrunkeji.com".force_encoding(Encoding::UTF_8), user_jl.phone)
				project = Project.new
				project.name = params[:project_name]			
				project.project_desc = params[:project_desc]
				project.project_manager = user.id
				project.score = 0
				project.score1 = 0
				project.score2 = 0
				project.score3 = 0
				project.lat = 0
				project.lng = 0
				project.create_at = Time.now
				project.update_at = Time.now
				project.area_radius = 1
				project.save
				user_detail.project_id = project.id
				user_detail.save
				project_team01 = ProjectTeam.new
				project_team02 = ProjectTeam.new
				project_team01.team_name = "项目经理组"
				project_team02.team_name = "监视员组"
				project_team01.project_id = project.id
				project_team02.project_id = project.id
				project_team01.save
				project_team02.save
				user_jl = User.find(params[:user_id])
				user_jl_detail = UserDetail.where(user_id: user_jl.id).first
				user_jl_detail.project_id = project.id
				user_jl_detail.save
				project_user = ProjectUser.new
				project_user.project_id = project.id
				project_user.user_id = user_jl.id
				project_user.role = 1
				project_user.join_time = Time.now
				project_user.save
				ProjectApi::make_project_user(project.id)
				{
					code: '0',
					msg: 'OK',
					project_id: project.id.to_s
				}
			end
		end
		########><><><><><><><><><><><>通用方法<><><><><><><><><><>#############
		#### 更新项目人员文件方法
		def make_project_user (id)
			@project_user = Hash.new
			all_users = []
			# 拿到所有加入组织的人员名单并去查询用户接口
			project_users = ProjectUser.where("project_id = ? and join_time IS NOT NULL", id)
			project_users.each do |pu|
				user = Hash.new
				user_object = User.where(id: pu.user_id).first
				user_detail_object = UserDetail.where(user_id: pu.user_id).first
				user["name"] = (user_object.name.nil? ? "" : user_object.name)
				user["user_id"] = (pu.user_id.nil? ? "" : pu.user_id)
				user["user_heard"] = (user_detail_object.head_img_url.nil? ? "" : user_detail_object.head_img_url)
				user["join_time"] = (pu.join_time.nil? ? "" : pu.join_time.strftime("%Y-%m-%d"))
				user["exit_time"] = (pu.exit_time.nil? ? "" : pu.exit_time.strftime("%Y-%m-%d"))
				user["real_name_status"] = user_detail_object.real_name_status
				user["upload_status"] = pu.upload_status
				all_users << user
			end
			@project_user["users"] = all_users
			teams_users = []
			# 拿到所有组织，并把组织下的人员展示出来
			project_teams = ProjectTeam.where(project_id: id)
			project_teams.each do |pt|
				if pt.team_name == "项目经理组" ||  pt.team_name == "监视员组"
					puts "ok"
				else
					user_ids = TeamUser.where(team_id: pt.id)
					team_user = Hash.new
					team_user["team_name"] = (pt.team_name.nil? ? "" : pt.team_name)
					team_user["team_manager"] = (pt.team_manager.nil? ? -1 : pt.team_manager)
					team_user["team_id"] = (pt.id.nil? ? -1 : pt.id)
					# user_id_list = []
					team_user["team_users"] = []
					user_ids.each do |ui|
						user_id = Hash.new
						user_id["user_id"] = (ui.user_id.nil? ? -1 : ui.user_id)
						team_user["team_users"] << user_id
					end
					teams_users << team_user
				end
			end
			# 项目经理组及监视员组
			project_manager = ProjectUser.where(project_id: id, role: 1)
			team_user = Hash.new
			team_user["team_name"] = "项目经理组"
			team_user["team_manager"] = -1
			team_user["team_id"] = -99
			team_user["team_users"] = []
			project_manager.each do |pm|
				user_id = Hash.new
				user_id["user_id"] = (pm.user_id.nil? ? -1 : pm.user_id)
				team_user["team_users"] << user_id
			end
			teams_users << team_user
			project_boss = ProjectUser.where(project_id: id, role: 2)
			team_user = Hash.new
			team_user["team_name"] = "监视员组"
			team_user["team_manager"] = -1
			team_user["team_id"] = -98
			team_user["team_users"] = []
			project_boss.each do |pb|
				user_id = Hash.new
				user_id["user_id"] = ( pb.user_id.nil? ? -1 : pb.user_id)
				team_user["team_users"] << user_id
			end
			teams_users << team_user
			# puts @project_user
			@project_user["teams"] =  teams_users
			puts @project_user.to_json
			file_path = '/home/deploy/hanrunkeji/' + 'projectuser_' + id.to_s + '.json'
			file_name = 'projectuser_' + id.to_s + '.json'
			f = File.new(file_path,'w')
			f.chmod(0777)
			File.open(file_path,"w") do |file|
			  file.write(@project_user.to_json)
			end
			code = CommonApi::path_post(file_path, file_name)
			if code == 200
				return "http://file.hanrunkeji.com" + "/" + file_name
			else
				return ""
			end
		end

		#### 查询用户签到记录 ####
		def get_sign(year, month, project_id, user_id)
			result = Hash.new
			result["code"] = "0"
			result["msg"] = "OK"
			today = Time.now
			signs = Sign.where(year: year, month: month, project_id: project_id, user_id: user_id).group("day")
			sign_list = []
			signs.each do |s|
				sign_list << s.create_at.strftime("%Y-%m-%d")
			end
			result["signs"] = sign_list
			all_day = CommonApi.month_days(year, month).to_i
			sign_day = signs.length
			if today.year == year.to_i && today.month == month.to_i
				unsign_day = today.day.to_i - sign_day.to_i
			elsif today.year < year.to_i
				unsign_day = 0
			elsif today.year == year.to_i && today.month > month.to_i
				unsign_day = all_day.to_i - sign_day.to_i
			elsif today.year == year.to_i && today.month < month.to_i
				unsign_day = 0
			elsif today.year > year.to_i
				unsign_day = all_day.to_i - sign_day
			end
			result["signed_num"] = sign_day
			result["unsiged_num"] = unsign_day
			return result
		end
		########建立人员和工作的关系
		def self.create_work_user(user_id, project_id, work_id, work_type, user_type, confirm_status)
			user = User.where(id: user_id).first
			work_user = WorkUser.where(user_id: user_id, work_id: work_id, user_type: user_type).first
			if work_user.nil? && !(user.nil?)
				work_user = WorkUser.new
				work_user.work_id = work_id
				work_user.user_id = user_id
				work_user.user_type = user_type
				work_user.project_id = project_id
				work_user.work_type = work_type
				work_user.confirm_status = confirm_status
				work_user.created_at = Time.now
				work_user.updated_at = Time.now
				if user_type == 1 && work_type == 1 
					event_id  = EventApi.make_event(user_id, project_id, 1, work_id)
					puts  event_id
					work_user.event_id = event_id
				# elsif user_type == 1 && work_type == 2
				# 	event_id  = EventApi.make_event(user_id, project_id, 2, work_id)
				# 	puts  event_id
				# 	work_user.event_id = event_id
				elsif user_type == 3 && work_type == 2 
					event_id  = EventApi.make_event(user_id, project_id, 2, work_id)
					work_user.event_id = event_id
				end
				if work_user.save
					return 1
				else
					return -1
				end
			else
				return -1
			end
		end

		########建立人员和评价的关系
		def self.create_evaluation_user(user_id, project_id, evaluation_id, user_type, confirm_status)
			user = User.where(id: user_id).first
			evaluation_user = EvaluationUser.where(user_id: user_id, evaluation_id: evaluation_id, user_type: user_type).first
			if evaluation_user.nil? && !(user.nil?)
				evaluation_user = EvaluationUser.new
				evaluation_user.evaluation_id = evaluation_id
				evaluation_user.user_id = user_id
				evaluation_user.user_type = user_type
				evaluation_user.project_id = project_id
				evaluation_user.confirm_status = confirm_status
				evaluation_user.created_at = Time.now
				evaluation_user.updated_at = Time.now
				if user_type == 1 
					evaluation_user.event_id = EventApi.make_event(user_id, project_id, 3, evaluation_id)
				end
				if evaluation_user.save
					return 1
				else
					return -1
				end
			else
				return -1
			end
		end
		########生成工作数据######
		def self.build_work(work_users)
			work_list = []
			work_users.each do |wu|
				work_detail = Hash.new
				work = Work.where(id: wu.work_id).first
				work_detail["id"] = work.id
				work_detail["work"] = work.content
				work_detail["work_type"] = work.work_type
				work_detail["publish_user"] = work.publish_user
				work_detail["publish_time"] = (work.created_at).strftime("%Y-%m-%d %H:%M")
				work_list << work_detail
			end
			return work_list
		end

		def self.build_admin_work(works)
			work_list = []
			works.each do |w|
				work_detail = Hash.new
				work_detail["id"] = w.id
				work_detail["work"] = w.content
				work_detail["work_type"] = w.work_type
				work_detail["publish_user"] = w.publish_user
				work_detail["publish_time"] = (w.created_at).strftime("%Y-%m-%d %H:%M")
				work_list << work_detail
			end
			return work_list
		end
		########生成评价数据######
		def self.build_evaluation(evaluations_users)
			evaluation_list = []
			evaluations_users.each do |eu|
				evaluation_detail = Hash.new
				evaluation = Evaluation.where(id: eu.evaluation_id).first
				evaluation_detail["id"] = evaluation.id
				evaluation_detail["score"] = evaluation.score.nil?? 0 : evaluation.score
				evaluation_detail["verified_type"] = evaluation.verified_type
				evaluation_detail["publish_user"] = evaluation.publish_user
				evaluation_detail["publish_time"] = (evaluation.created_at).strftime("%Y-%m-%d %H:%M")
				evaluation_list << evaluation_detail
			end
			return evaluation_list
		end

		def self.build_admin_evaluation(evaluations_users)
			evaluation_list = []
			evaluations_users.each do |eu|
				evaluation_detail = Hash.new
				evaluation_detail["id"] = eu.id
				evaluation_detail["score"] = eu.score.nil?? 0 : eu.score,
				evaluation_detail["verified_type"] = eu.verified_type
				evaluation_detail["publish_user"] = eu.publish_user
				evaluation_detail["publish_time"] = (eu.created_at).strftime("%Y-%m-%d %H:%M")
				evaluation_list << evaluation_detail
			end
			return evaluation_list
		end

		########生成评价数据######
		def self.build_evaluation01(evaluations_users)
			evaluation_list = []
			evaluations_users.each do |eu|
				evaluation_detail = Hash.new

				evaluation = Evaluation.where(id: eu.evaluation_id).first
				project = Project.where(id: eu.project_id).first
				user = User.where(id: evaluation.publish_user).first
				if user.nil? || project.nil? || evaluation.nil? || evaluation.verified_type != 2

				else
					evaluation_detail["id"] = evaluation.id
					evaluation_detail["score"] = evaluation.score.nil?? 0 : evaluation.score
					evaluation_detail["publish_user"] = user.name.nil? ? "" : user.name
					evaluation_detail["publish_time"] = (evaluation.created_at).strftime("%Y-%m-%d %H:%M")
					evaluation_detail["project_id"] = project.id
					evaluation_detail["project_name"] = project.name
					evaluation_list << evaluation_detail
				end
				
			end
			return evaluation_list
		end
		def self.build_admin_evaluation01(evaluations_users)
			evaluation_list = []
			evaluations_users.each do |eu|
				evaluation_detail = Hash.new
				project = Project.where(id: eu.project_id).first
				user = User.where(id: eu.publish_user).first
				if user.nil? || project.nil?

				else
					evaluation_detail["id"] = eu.id
					evaluation_detail["score"] = eu.score.nil?? 0 : eu.score
					evaluation_detail["publish_user"] = user.name.nil? ? "" : user.name
					evaluation_detail["publish_time"] = (eu.created_at).strftime("%Y-%m-%d %H:%M")
					evaluation_detail["project_id"] = project.id
					evaluation_detail["project_name"] = project.name
					evaluation_list << evaluation_detail
				end
				
			end
			return evaluation_list
		end
		def self.calculate_project_score
			projects = Project.all.count
			projects.each do |p|
				scores = 0
				score_count = 0 
				project_score = Evaluation.where(project_id: p.id)
				score_count = project_score.count 
				if score_count > 0
					project_score.each do |ps|
						if ps.verified_type == 2
							scores = ps.score.to_i + socores
						end
						
					end
					score = scores / score_count
					project.score = score
					project.save
				end
			end
		end

		def self.project_score(project_id)
			project_score = Evaluation.where(project_id: project_id, verified_type: 2)
			score_count =  Evaluation.where(project_id: project_id, verified_type: 2).count
			score = 0 
			if score_count > 0
				scores = 0
				project_score.each do |ps|
					if ps.verified_type == 2
						scores = ps.score.to_i + scores
					end			
				end
				score = scores / score_count
			end
			if score == 0
				score = -1
			end
			return score
		end
		#def self.create_upload_information(project_id)
		#	project = Project.find(project_id)
		#	excel_name = project.name + '公示文件' + '.xlsx'
		#	workbook  = WriteXLSX.new(excel_name)
		#	worksheet1 = workbook.add_worksheet('企业信息上报') # 写企业信息上报
#
		#	format_s1bt = workbook.add_format
		#	format_s1bt.set_color('red')
		#	format_s1bt.set_align('center')
		#	worksheet1.write('A1', '企业名称', format_s1bt)
		#	worksheet1.write('A2', '统一社会编码', format_s1bt)
		#	worksheet1.write('A3', '单位性质', format_s1bt)
		#	binding.pry
		#	workbook.close
#
		#	sheet_url1 = CommonApi::URL_QN + "/" + excel_name
		#	code01 = CommonApi::url_post(workbook, excel_name)

			#worksheet2 = workbook.add_worksheet('项目信息上报')    # Foglio2
			#worksheet3 = workbook.add_worksheet('人员信息上报')       # Data

		#end
	end
end
